-
Notifications
You must be signed in to change notification settings - Fork 7
Migration guide
Joe Sweeney edited this page Jul 25, 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, {
expireInSeconds: 900 // expire session in 15 minutes
cookieSetOptions: {}
cookieGetOptions: {}
});
// 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);
// Call `initMiddleware` statically, and feed the store and other options as arguments to `initMiddleware` (make sure the S in `Session` is capitalized)
app.use(Session.initMiddleware(store, {
expireInSeconds: 900 // expire session in 15 minutes
cookieSetOptions: {}
cookieGetOptions: {}
}))
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 a session ID or an Oak context as an argument.