Create admin page
This commit is contained in:
@@ -3,6 +3,7 @@ import { createContext, useState, useEffect } from 'react';
|
||||
import { TokenContextType, TokenType } from '@/types/user';
|
||||
import { TokenProps } from '@/types/user';
|
||||
import { useRouter, usePathname } from '@/src/navigation';
|
||||
import { roles } from '@/config/selection';
|
||||
const LOCAL_STORAGE_KEY = 'testplat-auth-token';
|
||||
|
||||
function storeTokenToLocalStorage(token: TokenType) {
|
||||
@@ -19,6 +20,7 @@ const defaultContext = {
|
||||
user: null,
|
||||
},
|
||||
isSignedIn: () => false,
|
||||
isAdmin: () => false,
|
||||
setToken: (token: TokenType) => {},
|
||||
storeTokenToLocalStorage,
|
||||
removeTokenFromLocalStorage,
|
||||
@@ -50,9 +52,26 @@ const TokenProvider = ({ locale, children }: TokenProps) => {
|
||||
return false;
|
||||
};
|
||||
|
||||
const isAdmin = () => {
|
||||
// check token
|
||||
if (token && token.user && token.user.username) {
|
||||
// check expire date
|
||||
if (Date.now() < token.expires_at) {
|
||||
// check role
|
||||
const adminRoleIndex = roles.findIndex((entry) => entry.uid === 'administrator');
|
||||
if (token.user.role === adminRoleIndex) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
const tokenContext = {
|
||||
token,
|
||||
isSignedIn,
|
||||
isAdmin,
|
||||
setToken,
|
||||
storeTokenToLocalStorage,
|
||||
removeTokenFromLocalStorage,
|
||||
|
||||
Reference in New Issue
Block a user