Skip to main content

Get User Info

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: "[email protected]"});}

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})    // ... })

On the Frontend#

Step 1: Wrap the component in which you want to get the info with PasswordlessAuth#

import React from "react";import { PasswordlessAuth } from 'supertokens-auth-react/recipe/passwordless';import Dashboard from './dashboard';
function App(props: any) {     return (           <PasswordlessAuth>                  <Dashboard />           </PasswordlessAuth>     );}

Step 2: This is how to use the session context in a component:#

import React from "react";import { useSessionContext } from 'supertokens-auth-react/recipe/session'; 
// Your dashboard componentfunction Dashboard(props: any) {    let {userId, accessTokenPayload} = useSessionContext();
    let role = accessTokenPayload.role;
    if (role === "admin") {        // TODO..    } else {        // TODO..    }}