Skip to main content

Get user information on the frontend

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

import React from "react";import { SessionAuth } from 'supertokens-auth-react/recipe/session';import Dashboard from "./dashboard";
function ProtectedDashboard(props: any) {      return (            <SessionAuth requireAuth={true} redirectToLogin={() => {/* ... */ }}>                  <Dashboard />            </SessionAuth>      );}

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