Skip to content

Session Cookie

The WSO2 Identity Server Express SDK keeps track of each user's session with a cookie named ASGARDEO_SESSION_ID. The cookie holds a random session ID. The user's tokens stay on the server, in the SDK's store, keyed by that ID.

  • The sign-in route sets the cookie just before it redirects the user to WSO2 Identity Server. If the request already has the cookie, the SDK keeps its value. Otherwise, it creates a new session ID.
  • The sign-in route, the sign-out route and protectRoute() read the cookie from req.cookies. Express does not parse cookies on its own, so add a cookie parser such as cookie-parser before the SDK's middleware:

    import cookieParser from "cookie-parser";
    
    app.use(cookieParser());
    
  • The sign-out route clears the cookie before it redirects the user to WSO2 Identity Server.

To read the user's information or tokens in your routes, pass the cookie's value to the client:

const accessToken = await req.asgardeoAuth.getAccessToken(req.cookies.ASGARDEO_SESSION_ID);

Set the cookie's options with cookieConfig when you create the client. The SDK passes them to Express's res.cookie().

AsgardeoExpressClient.getInstance({
  appURL: "https://app.example.com",
  baseUrl: "https://localhost:9443",
  clientId: "<your-app-client-id>",
  clientSecret: "<your-app-client-secret>",
  cookieConfig: {
    maxAge: 8 * 60 * 60 * 1000, // 8 hours
    secure: true
  }
});
Option Type Default Description
maxAge number 86400000 (24 hours) How long the cookie lasts, in milliseconds.
httpOnly boolean true Keep the cookie out of reach of JavaScript in the browser.
sameSite string "lax" The cookie's SameSite attribute: "lax", "strict" or "none".
secure boolean false Only send the cookie over HTTPS.

Tip

The default for secure is false, so the cookie also works on http://localhost. Set it to true when your app runs over HTTPS in production.

Note

When the access token expires, isSignedIn() and protectRoute() refresh it with the refresh token, so the user stays signed in for as long as the cookie lasts. Enable the refresh token grant for the application in WSO2 Identity Server. Without a refresh token, the user is treated as signed out once the access token expires, even if the cookie is still there.