Skip to main content

5. Session verification / Building your APIs

For this guide, we will assume that we want an API /user GET which returns the current session information.

1) Create a new file user.js#

An example of this is here.

2) Call the supertokens.init function#

Remember that whenever we want to use any functions from the supertokens-node lib, we have to call the supertokens.init function at the top of that serverless function file.

user.js
let supertokens = require("supertokens-node");let { getBackendConfig } = require("./config");
supertokens.init(getBackendConfig())

3) Use session verification with your handler#

We use the verifySession() to verify a session.

user.js
let supertokens = require("supertokens-node");let { getBackendConfig } = require("../../config/supertokensConfig");let { verifySession } = require("supertokens-node/recipe/session/framework/awsLambda");let middy = require("@middy/core");let cors = require("@middy/http-cors");
supertokens.init(getBackendConfig());
const handler = async (event, context) => {    return {        body: JSON.stringify({            sessionHandle: event.session.getHandle(),            userId: event.session.getUserId(),            jwtPayload: event.session.getJWTPayload()        })    }}
module.exports.handler = middy(verifySession(handler)).use(cors({    origin: getBackendConfig().appInfo.websiteDomain,    credentials: true,    headers: ["Content-Type", ...supertokens.getAllCORSHeaders()].join(", "),    methods: "OPTIONS,POST,GET,PUT,DELETE"}));

4) Configure API Gateway#

Please fill the form below to see the code snippet (* = Required)
Website Domain:*
This is the URL of your website, without any path.
Fill form to submit