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


import ThirdPartyPasswordless from "supertokens-node/recipe/thirdpartypasswordless";

async function handler() {
const userInfo = await ThirdPartyPasswordless.getUsersByEmail("test@example.com");
}

Using getUserByPhoneNumber#

import ThirdPartyPasswordless from "supertokens-node/recipe/thirdpartypasswordless";

async function handler() {
const userInfo = await ThirdPartyPasswordless.getUserByPhoneNumber({phoneNumber: "+1234567891"});
}

Using getUserById#

import express from "express";

import ThirdPartyPasswordless from "supertokens-node/recipe/thirdpartypasswordless";
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 ThirdPartyPasswordless.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;
}

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