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 ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";
async function getUserInfo() {    // Note that usersInfo has type User[]    // You can learn more about the `User` object over here https://github.com/supertokens/core-driver-interface/wiki    let usersInfo = await ThirdPartyEmailPassword.getUsersByEmail("[email protected]");}

Using getUserById#

import express from "express";import ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";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 https://github.com/supertokens/core-driver-interface/wiki    let userInfo = await ThirdPartyEmailPassword.getUserById(userId)    // ... })

On the Frontend#

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

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

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