How can I declare session types with cookie-session?

94 views Asked by At

I am trying to migrate from express-session to cookie-session because I want client-side sessions. For express session I would just simply write on app.ts:

declare module 'express-session' {
  interface SessionData {
    isSuperAdmin: boolean;
    token: string;
    operatorId: number;
    providerId?: number;
  }
}

app.use(
  session({
    secret: process.env.SESSION_SECRET as string,
    resave: false,
    saveUninitialized: false
  })
);

This would let me use req.session.isSuperAdmin in typescript. But I don´t know how to do it in cookie-session. I setup cookie-session with:

cookieSession({
  secret: process.env.SESSION_SECRET as string,
});

It works, but I can´t use req.session.isSuperAdmin without first checking that attribute exists. How could I declare session´s type so I don´t have to check for every attribute I want to use.

0

There are 0 answers