Reading roles on the frontend
To access the user's role on the frontend we read from the access token payload from the session
- ReactJS
- Angular
- Plain JavaScript
- React Native
- With React Context
- Without React Context
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.. } }}
import Session from 'supertokens-auth-react/recipe/session';
async function getRoles() { if (await Session.doesSessionExist()) {
// we use the key "roles" here since that's what we // used while setting the payload in the backend. let roles: string[] = (await Session.getAccessTokenPayloadSecurely())["roles"]; }}
- Via NPM
- Via Script Tag
import Session from 'supertokens-web-js/recipe/session';
async function getRoles() { if (await Session.doesSessionExist()) {
// we use the key "roles" here since that's what we // used while setting the payload in the backend. let roles: string[] = (await Session.getAccessTokenPayloadSecurely())["roles"]; }}
async function getRoles() { if (await supertokensSession.doesSessionExist()) {
// we use the key "roles" here since that's what we // used while setting the payload in the backend. let roles: string[] = (await supertokensSession.getAccessTokenPayloadSecurely())["roles"]; }}
import SuperTokens from 'supertokens-react-native';
async function getRole() { if (await SuperTokens.doesSessionExist()) {
// we use the key "roles" here since that's what we // used while setting the payload in the backend. let roles: string[] = (await SuperTokens.getAccessTokenPayloadSecurely())["roles"]; }}
import Session from 'supertokens-web-js/recipe/session';
async function getRoles() { if (await Session.doesSessionExist()) {
// we use the key "roles" here since that's what we // used while setting the payload in the backend. let roles: string[] = (await Session.getAccessTokenPayloadSecurely())["roles"]; }}