Skip to main content

Update Session Data

Method 1) After session verification#

let { verifySession } = require("supertokens-node/recipe/session/framework/express");"/updateinfo", verifySession(), async (req, res) => {
    let session = req.session;
    let currSessionData = session.getSessionData();
    await session.updateSessionData(        {newKey: "newValue", ...currSessionData}    );
    res.json({message: "successfully updated Session data in the database"})});
  • We first require session verification in order to get the session object
  • Using that object, we call the updateSessionData with new content. This content completely overrides the existing object, that's why we first get the currSessionData info.
  • The result is that the session data stored in the database (against the verified session) is updated. The change is instantly visible to other calls of getSessionData for this session.

Method 2) Without session verification#

let Session = require("supertokens-node/recipe/session");
// we first get all the sessionHandles (string[]) for a userlet sessionHandles = Session.getAllSessionHandlesForUser(userId);
// we update all the session's data for this usersessionHandles.forEach(async (handle) => {      let currSessionData = (await Session.getSessionInformation(handle)).sessionData;
      await Session.updateSessionData(handle,             {newKey: "newValue", ...currSessionData}      );})