Skip to content

Migration guide

Joe Sweeney edited this page Jul 25, 2022 · 6 revisions

Migrating from 3.x to 4.x

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.

Before

import { Session, RedisStore } from "https://deno.land/x/oak_sessions/mod.ts";
import { connect } from 'https://deno.land/x/redis@v0.25.0/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())
// ...

Now

Change the above to this:

import { Session, RedisStore } from "https://deno.land/x/oak_sessions/mod.ts";
import { connect } from 'https://deno.land/x/redis@v0.25.0/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.

Clone this wiki locally