Skip to content

Commit

Permalink
fix: wildcard route check for /admin routes
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-nowicki committed Aug 17, 2024
1 parent e0e9a0c commit 7bbd79a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions api/apiFunctions.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface IUser {
id: string;
email: string;
leagues: string[];
labels: string[];
}
export interface IUserPick {
[userId: string]: {
Expand Down
15 changes: 8 additions & 7 deletions context/AuthContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,7 +39,6 @@ export const AuthContextProvider = ({
children: React.ReactNode;
}): JSX.Element => {
const [isSignedIn, setIsSignedIn] = useState<boolean>(false);
const [isSuperAdmin, setIsSuperAdmin] = useState<boolean>(false);
const { updateUser, resetUser, user } = useDataStore<DataStore>(
(state) => state,
);
Expand All @@ -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]);

Expand Down Expand Up @@ -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();
Expand Down
9 changes: 0 additions & 9 deletions lib/adminRoutes.ts

This file was deleted.

7 changes: 7 additions & 0 deletions store/dataStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const userData = {
userId: '123',
userEmail: '[email protected]',
leagues: ['123456'],
labels: ['admin'],
};

const NFLTeam = [
Expand Down Expand Up @@ -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());
Expand All @@ -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([]);
});
});

Expand Down
5 changes: 4 additions & 1 deletion store/dataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface IDataStoreAction {
id: IUser['id'],
email: IUser['email'],
leagues: IUser['leagues'],
labels: IUser['labels'],
) => void;
updateWeeklyPicks: ({
leagueId,
Expand All @@ -56,6 +57,7 @@ const initialState: IDataStoreState = {
id: '',
email: '',
leagues: [],
labels: [],
},
weeklyPicks: {
leagueId: '',
Expand Down Expand Up @@ -101,12 +103,13 @@ export const useDataStore = create<DataStore>((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];
}),
),
/**
Expand Down
9 changes: 9 additions & 0 deletions utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,12 @@ export const getUserLeagues = async (
export const getUserEntries = async (userId: IUser['id'], leagueId: ILeague['leagueId']): Promise<IEntry[]> => {
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');
};

0 comments on commit 7bbd79a

Please sign in to comment.