From 7bbd79a185352a2be910cfc8b842e97affb0aa6d Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Sat, 17 Aug 2024 12:35:36 -0400 Subject: [PATCH] fix: wildcard route check for /admin routes --- api/apiFunctions.interface.ts | 1 + context/AuthContextProvider.tsx | 15 ++++++++------- lib/adminRoutes.ts | 9 --------- store/dataStore.test.ts | 7 +++++++ store/dataStore.ts | 5 ++++- utils/utils.ts | 9 +++++++++ 6 files changed, 29 insertions(+), 17 deletions(-) delete mode 100644 lib/adminRoutes.ts diff --git a/api/apiFunctions.interface.ts b/api/apiFunctions.interface.ts index 7d54c407..fa09f434 100644 --- a/api/apiFunctions.interface.ts +++ b/api/apiFunctions.interface.ts @@ -11,6 +11,7 @@ export interface IUser { id: string; email: string; leagues: string[]; + labels: string[]; } export interface IUserPick { [userId: string]: { diff --git a/context/AuthContextProvider.tsx b/context/AuthContextProvider.tsx index 13237b30..19b3e7ed 100644 --- a/context/AuthContextProvider.tsx +++ b/context/AuthContextProvider.tsx @@ -12,7 +12,6 @@ import { IUser } from '@/api/apiFunctions.interface'; import { getCurrentUser } from '@/api/apiFunctions'; import { loginAccount, logoutHandler } from './AuthHelper'; import { usePathname } from 'next/navigation'; -import { adminRoutes } from '@/lib/adminRoutes'; type UserCredentials = { email: string; @@ -40,7 +39,6 @@ export const AuthContextProvider = ({ children: React.ReactNode; }): JSX.Element => { const [isSignedIn, setIsSignedIn] = useState(false); - const [isSuperAdmin, setIsSuperAdmin] = useState(false); const { updateUser, resetUser, user } = useDataStore( (state) => state, ); @@ -56,10 +54,8 @@ export const AuthContextProvider = ({ }, [user]); useMemo(() => { - if (isSignedIn) { - if (adminRoutes.includes(pathname)) { - !isSuperAdmin && router.push('/league/all'); - } + if (pathname.startsWith('/admin')) { + !user.labels.includes('admin') && router.push('/league/all'); } }, [pathname]); @@ -103,7 +99,12 @@ export const AuthContextProvider = ({ setIsSuperAdmin(true); } const userData: IUser = await getCurrentUser(user.$id); - updateUser(userData.id, userData.email, userData.leagues); + updateUser( + userData.id, + userData.email, + userData.leagues, + userData.labels, + ); return userData; } catch (error) { resetUser(); diff --git a/lib/adminRoutes.ts b/lib/adminRoutes.ts deleted file mode 100644 index 6188769e..00000000 --- a/lib/adminRoutes.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) Gridiron Survivor. -// Licensed under the MIT License. - -export const adminRoutes: string[] = [ - '/admin', - '/admin/leagues', - '/admin/players', - '/admin/notifications', -]; diff --git a/store/dataStore.test.ts b/store/dataStore.test.ts index 364f336e..33103bd5 100644 --- a/store/dataStore.test.ts +++ b/store/dataStore.test.ts @@ -6,6 +6,7 @@ const userData = { userId: '123', userEmail: 'test@email.com', leagues: ['123456'], + labels: ['admin'], }; const NFLTeam = [ @@ -50,11 +51,14 @@ describe('Data Store', () => { userData.userId, userData.userEmail, userData.leagues, + userData.labels, ); }); expect(result.current.user.id).toBe(userData.userId); expect(result.current.user.email).toBe(userData.userEmail); + expect(result.current.user.labels).toStrictEqual(userData.labels); + expect(result.current.user.leagues).toStrictEqual(userData.leagues); }); it('Checks the reset user state matches default', () => { const { result } = renderHook(() => useDataStore()); @@ -64,12 +68,15 @@ describe('Data Store', () => { userData.userId, userData.userEmail, userData.leagues, + userData.labels, ); result.current.resetUser(); }); expect(result.current.user.id).toBe(''); expect(result.current.user.email).toBe(''); + expect(result.current.user.labels).toStrictEqual([]); + expect(result.current.user.leagues).toStrictEqual([]); }); }); diff --git a/store/dataStore.ts b/store/dataStore.ts index 9832e89b..88239b77 100644 --- a/store/dataStore.ts +++ b/store/dataStore.ts @@ -30,6 +30,7 @@ interface IDataStoreAction { id: IUser['id'], email: IUser['email'], leagues: IUser['leagues'], + labels: IUser['labels'], ) => void; updateWeeklyPicks: ({ leagueId, @@ -56,6 +57,7 @@ const initialState: IDataStoreState = { id: '', email: '', leagues: [], + labels: [], }, weeklyPicks: { leagueId: '', @@ -101,12 +103,13 @@ export const useDataStore = create((set) => ({ * @param leagues - The user league * @returns {void} */ - updateUser: (id, email, leagues): void => + updateUser: (id, email, leagues, labels): void => set( produce((state: IDataStoreState) => { state.user.id = id; state.user.email = email; state.user.leagues = [...leagues]; + state.user.labels = [...labels]; }), ), /** diff --git a/utils/utils.ts b/utils/utils.ts index 3110957b..f59eba22 100644 --- a/utils/utils.ts +++ b/utils/utils.ts @@ -144,3 +144,12 @@ export const getUserLeagues = async ( export const getUserEntries = async (userId: IUser['id'], leagueId: ILeague['leagueId']): Promise => { return await getCurrentUserEntries(userId, leagueId); } + +/** + * Check if the route is an /admin route + * @param path - The path to check + * @returns {boolean} - Whether the route is an /admin route + */ +export const isAdminRoute = (path: string): boolean => { + return path.startsWith('/admin'); +};