Skip to main content

Reading roles on the frontend

To access the user's role on the frontend we read from the access token payload from the session

import React from "react";import { useSessionContext } from 'supertokens-auth-react/recipe/session';
function Dashboard(props: any) {      let session = useSessionContext();
      if (session.loading) {        return null;      }
      if (!session.doesSessionExist) {        // TODO      } else {        // we use the key "roles" here since that's what we        // used while setting the payload in the backend.         let roles: string[] = session.accessTokenPayload.roles;
        if (roles.includes("admin")) {                // TODO..        } else {                // TODO..        }      }}