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.
user.js
#
1) Create a new file An example of this is here.
supertokens.init
function#
2) Call the 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.ts
import supertokens from "supertokens-node";import { getBackendConfig } from "./config";
supertokens.init(getBackendConfig())
#
3) Use session verification with your handlerWe use the verifySession()
to verify a session.
user.ts
import supertokens from "supertokens-node";import { getBackendConfig } from "../../config/supertokensConfig";import { verifySession } from "supertokens-node/recipe/session/framework/awsLambda";import { SessionEvent } from "supertokens-node/framework/awsLambda";import middy from "@middy/core";import cors from "@middy/http-cors";
supertokens.init(getBackendConfig());
const handler = async (event: SessionEvent) => { return { body: JSON.stringify({ sessionHandle: event.session!.getHandle(), userId: event.session!.getUserId(), accessTokenPayload: event.session!.getAccessTokenPayload() }) }}
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"})).onError(request => { throw request.error;});
#
4) Configure API GatewayPlease fill the form below to see the code snippet (* = Required)
To learn more about what these properties mean read here.
Website Domain:*

This is the URL of your website.