Skip to main content

Get User Info

On the Backend#

Using getUserByEmail#

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

import ThirdParty from "supertokens-node/recipe/thirdparty";
async function getUserInfo() {    // Note that usersInfo has type User[]    // You can learn more about the `User` object over here    let usersInfo = await ThirdParty.getUsersByEmail("[email protected]");}

Using getUserById#

import express from "express";import ThirdParty from "supertokens-node/recipe/thirdparty";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 ThirdParty.getUserById(userId)    // ... })

On the Frontend#

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

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

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