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, getUserByPhoneNumber and getUserById functions:


import Passwordless from "supertokens-node/recipe/passwordless";
async function handler() {    const userInfo = await Passwordless.getUserByEmail({email: "test@example.com"});}

Using getUserByPhoneNumber#

import Passwordless from "supertokens-node/recipe/passwordless";
async function handler() {    const userInfo = await Passwordless.getUserByPhoneNumber({phoneNumber: "+1234567891"});}

Using getUserById#

import express from "express";
import Passwordless from "supertokens-node/recipe/passwordless";import { verifySession } from "supertokens-node/recipe/session/framework/express";
const app = express();
app.get("/get-user-info", verifySession(), async (req, res) => {    let userId = req.session.getUserId();
    let userInfo = await Passwordless.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 componentfunction Dashboard(props: any) {    let session = useSessionContext();
    if (session.loading) {        return null;    }
    if (!session.doesSessionExist) {        // TODO    } else {        let {userId, accessTokenPayload} = session;
        let name = accessTokenPayload.name;    }}
Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react