Skip to main content

Update Session Data

Method 1) After session verification#

let Session = require("supertokens-node/recipe/session");"/updateinfo", Session.verifySession(), async (req, res) => {      let session = req.session;
      let currSessionData = await 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 req.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.getSessionData(handle);
      await Session.updateSessionData(handle,             {newKey: "newValue", ...currSessionData}      );})