https://supertokens.com/ logo
#support-questions
Title
# support-questions
r

rp

10/18/2021, 5:09 AM
Yea. So you can do something like this:
Copy code
js
import express from "express";
import bodyParser from "body-parser";
import { RegisterRoutes } from "../build/routes";
import supertokens from "supertokens-node";
import session from "supertokens-node/recipe/session";

export const app = express();

// Use body parser to read sent json payloads
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
);
app.use(bodyParser.json());

// exposes all the auth routes for the frontend to consume
app.use(supertokens.middleware());

// global session verification
app.use(session.verifySession({sessionRequired: false}), (req, res) => {
  if (req.session === undefined) {
    // no session exists
  } else {
    // session exists
    let userId = req.session.getUserId();
    // TODO: attach this userId to the request object and consume it later in your APIs?
  }
})

RegisterRoutes(app);