Skip to main content

Get User Info

You can fetch user information on the backend as well as on the frontend.

Fetching on the backend#

Using getUserByEmail#

You can get a user's information on the backend using the getUserByEmail and getUserById functions:

import EmailPassword from "supertokens-node/recipe/emailpassword";

async function getUserInfo() {
// Note that usersInfo has type User[]
// You can learn more about the `User` object over here https://github.com/supertokens/core-driver-interface/wiki
let usersInfo = await EmailPassword.getUserByEmail("test@example.com");
}

Using getUserById#

import express from "express";
import EmailPassword from "supertokens-node/recipe/emailpassword";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { SessionRequest } from 'supertokens-node/framework/express';

let app = express();
app.get("/get-user-info", verifySession(), async (req: SessionRequest, res) => {
let userId = req.session!.getUserId();
// You can learn more about the `User` object over here https://github.com/supertokens/core-driver-interface/wiki
let userInfo = await EmailPassword.getUserById(userId)
// ...
})

Fetching on the frontend#

important

The function calls below require no API calls and read directly from the session information stored on the frontend. This makes them very quick.

import React from "react";
import { useSessionContext } from 'supertokens-auth-react/recipe/session';

// Your dashboard component
function Dashboard(props: any) {
let session = useSessionContext();

if (session.loading) {
return null;
}

let {doesSessionExist, userId, accessTokenPayload} = session;

// doesSessionExist will always be true if this is wrapped in `<SessionAuth>`
if (!doesSessionExist) {
// TODO
}

let name = accessTokenPayload.userName;
}
Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react