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 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    let usersInfo = await EmailPassword.getUserByEmail("[email protected]");}

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    let userInfo = await EmailPassword.getUserById(userId)    // ... })

On the Frontend#

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

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

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