Skip to main content

Reading roles in the frontend

If a session exists on the frontend, we can read the role by getting information from the session:

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

import { ThirdPartyEmailPasswordAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword';
render() {     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';
function Dashboard(props) {      let {userId, jwtPayload} = useSessionContext();
      // we use the key "role" here since that's what we      // used while setting the payload in the backend.       let role = jwtPayload.role;
      if (role === "admin") {            // TODO..      } else {            // TODO..      }}
tip

You can pass requireAuth={false} to ThirdPartyEmailPasswordAuth if you want the <Dashboard /> component to load even if there is no session. In this case, you would need to check if a session exists in the <Dashboard /> component.