-
Notifications
You must be signed in to change notification settings - Fork 7
Migration guide
Joe Sweeney edited this page Jul 22, 2022
·
6 revisions
A couple changes in the bootstrapping code for the Session class are needed when switching from 3.x to 4.x of oak_sessions. We are no longer creating an instance that survives between requests; We are actually creating a brand new Session instance for each request, and calling the middleware from a static method.
import { Session, RedisStore } from "https://deno.land/x/oak_sessions/mod.ts";
import { connect } from 'https://deno.land/x/[email protected]/mod.ts'
const redis = await connect({
hostname: '0.0.0.0',
port: 6379
})
const store = new RedisStore(redis)
const session = new Session(store);
// In your middleware
app.use(session.initMiddleware())
// ...
Change the above to this:
import { Session, RedisStore } from "https://deno.land/x/oak_sessions/mod.ts";
import { connect } from 'https://deno.land/x/[email protected]/mod.ts';
const redis = await connect({
hostname: '0.0.0.0',
port: 6379
});
const store = new RedisStore(redis);
Session.store = store
// This also goes for cookie options, session expirition, etc.
Session.expiration = 900
Session.cookieSetOptions = {}
Session.cookieGetOptions = {}
// Call `initMiddleware` statically. Very similar to before, but the method is static now (make sure the S in `Session` is capitalized)
app.use(Session.initMiddleware())
The ctx.state.session
methods (such as get
, set
, has
, flash
and deleteSession
are called the same way as before, but they are synchronous methods now, so you don't need to put await
in front of them.
Also, deleteSession()
no longer takes any arguments. You can only delete from within a session context now, you can no longer supply an ID or an Oak context.