Getting the stored data
- NodeJS
- GoLang
- Python
- Other Frameworks
Important
For other backend frameworks, you can follow our guide on how to spin up a separate server configured with the SuperTokens backend SDK to authenticate requests and issue session tokens.
- Express
- Hapi
- Fastify
- Koa
- Loopback
- AWS Lambda / Netlify
- Next.js (Pages Dir)
- Next.js (App Dir)
- NestJS
import express from "express";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let app = express();
app.post("/updateinfo", verifySession(), async (req, res) => {
const session = req.session;
const userId = session.getUserId();
const { metadata } = await UserMetadata.getUserMetadata(userId);
res.json({ preferences: metadata.preferences });
});
import Hapi from "@hapi/hapi";
import { verifySession } from "supertokens-node/recipe/session/framework/hapi";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import { SessionRequest } from "supertokens-node/framework/hapi";
let server = Hapi.server({ port: 8000 });
server.route({
path: "/updateinfo",
method: "post",
options: {
pre: [
{
method: verifySession(),
},
],
},
handler: async (req: SessionRequest, res) => {
const session = req.session;
const userId = session!.getUserId();
const { metadata } = await UserMetadata.getUserMetadata(userId);
return res.response({ preferences: metadata.preferences }).code(200);
},
});
import Fastify from "fastify";
import { verifySession } from "supertokens-node/recipe/session/framework/fastify";
import UserMetadata from "supertokens-node/recipe/usermetadata";
let fastify = Fastify();
fastify.post(
"/updateinfo",
{
preHandler: verifySession(),
},
async (req, res) => {
const session = req.session;
const userId = session.getUserId();
const { metadata } = await UserMetadata.getUserMetadata(userId);
res.send({ preferences: metadata.preferences });
},
);
import { verifySession } from "supertokens-node/recipe/session/framework/awsLambda";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import { SessionEvent } from "supertokens-node/framework/awsLambda";
async function updateinfo(awsEvent: SessionEvent) {
const session = awsEvent.session;
const userId = session!.getUserId();
const { metadata } = await UserMetadata.getUserMetadata(userId);
return {
body: JSON.stringify({ preferences: metadata.preferences }),
statusCode: 200,
};
}
exports.handler = verifySession(updateinfo);
import KoaRouter from "koa-router";
import { verifySession } from "supertokens-node/recipe/session/framework/koa";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import { SessionContext } from "supertokens-node/framework/koa";
let router = new KoaRouter();
router.post("/updateinfo", verifySession(), async (ctx: SessionContext, next) => {
const session = ctx.session;
const userId = session!.getUserId();
const { metadata } = await UserMetadata.getUserMetadata(userId);
ctx.body = { preferences: metadata.preferences };
});
import { inject, intercept } from "@loopback/core";
import { RestBindings, post, response } from "@loopback/rest";
import { verifySession } from "supertokens-node/recipe/session/framework/loopback";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import { SessionContext } from "supertokens-node/framework/loopback";
class UpdateInfo {
constructor(@inject(RestBindings.Http.CONTEXT) private ctx: SessionContext) {}
@post("/updateinfo")
@intercept(verifySession())
@response(200)
async handler() {
const session = this.ctx.session;
const userId = session!.getUserId();
const { metadata } = await UserMetadata.getUserMetadata(userId);
return { preferences: metadata.preferences };
}
}
import { superTokensNextWrapper } from "supertokens-node/nextjs";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import { SessionRequest } from "supertokens-node/framework/express";
export default async function updateInfo(req: any, res: any) {
await superTokensNextWrapper(
async (next) => {
await verifySession()(req, res, next);
},
req,
res,
);
const session = (req as SessionRequest).session;
const userId = session!.getUserId();
const { metadata } = await UserMetadata.getUserMetadata(userId);
res.json({ preferences: metadata.preferences });
}
import { NextResponse, NextRequest } from "next/server";
import SuperTokens from "supertokens-node";
import { withSession } from "supertokens-node/nextjs";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import { backendConfig } from "@/app/config/backend";
SuperTokens.init(backendConfig());
export function POST(request: NextRequest) {
return withSession(request, async (err, session) => {
if (err) {
return NextResponse.json(err, { status: 500 });
}
const userId = session!.getUserId();
const { metadata } = await UserMetadata.getUserMetadata(userId);
return NextResponse.json({ preferences: metadata.preferences });
});
}
import { Controller, Post, UseGuards, Session } from "@nestjs/common";
import { SessionContainer } from "supertokens-node/recipe/session";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import { AuthGuard } from "./auth/auth.guard";
@Controller()
export class ExampleController {
// For more information about "AuthGuard" and the "Session" decorator please read our NestJS guide.
@Post("example")
@UseGuards(new AuthGuard())
async postExample(@Session() session: SessionContainer): Promise<{ preferences: any }> {
const userId = session.getUserId();
const { metadata } = await UserMetadata.getUserMetadata(userId);
return { preferences: metadata.preferences };
}
}
import (
"fmt"
"github.com/supertokens/supertokens-golang/recipe/usermetadata"
)
func main() {
userId := "..."
metadata, err := usermetadata.GetUserMetadata(userId)
if err != nil {
// TODO: handle error...
}
exampleValue := metadata["exampleKey"]
fmt.Println(exampleValue)
}
- Asyncio
- Syncio
from supertokens_python.recipe.usermetadata.asyncio import get_user_metadata
async def some_func():
user_id = "..."
metadataResult = await get_user_metadata(user_id)
exampleValue = metadataResult.metadata["exampleKey"]
print(exampleValue)
from supertokens_python.recipe.usermetadata.syncio import get_user_metadata
user_id = "..."
metadataResult = get_user_metadata(user_id)
exampleValue = metadataResult.metadata["exampleKey"]
print(exampleValue)
important
By default, all users have an empty metadata object.