Create admin page

This commit is contained in:
Takeshi Kimata
2024-05-26 20:50:23 +09:00
parent 7d071303dd
commit a573358a85
19 changed files with 330 additions and 14 deletions

View File

@@ -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,