-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chris/feat(#381): Implement User Role Access #458
Changes from 3 commits
e870455
3e5b4b0
9a4ea6b
e0e9a0c
7bbd79a
b9f9114
cc406d4
3cf0d8b
deeb21d
4fad4c4
8229a37
8f91a3e
a0c18ad
a3a6946
819f1fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ import { IUser } from '@/api/apiFunctions.interface'; | |||||
import { getCurrentUser } from '@/api/apiFunctions'; | ||||||
import { loginAccount } from './AuthHelper'; | ||||||
import { usePathname } from 'next/navigation'; | ||||||
import { adminRoutes } from '@/lib/adminRoutes'; | ||||||
|
||||||
type UserCredentials = { | ||||||
email: string; | ||||||
|
@@ -40,6 +41,7 @@ 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, | ||||||
); | ||||||
|
@@ -54,6 +56,14 @@ export const AuthContextProvider = ({ | |||||
setIsSignedIn(true); | ||||||
}, [user]); | ||||||
|
||||||
useMemo(() => { | ||||||
if (isSignedIn) { | ||||||
choir241 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if (adminRoutes.includes(pathname)) { | ||||||
!isSuperAdmin && router.push('/league/all'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The homepage should redirect them to league/all already. Once this is set to the real homepage, we won't need to modify this again.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shashilo tried this out and nope I am not redirected automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done ✅ |
||||||
} | ||||||
} | ||||||
}, [pathname]); | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we write out other edge cases such as
What are your thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vmaineng brings up a great point. If this is a case in our app, we should write it out by making a unit test for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ pathname helper function added to utils with a unit test to test pathname changing. |
||||||
/** | ||||||
* Authenticate and set session state | ||||||
* @param user - The user credentials. | ||||||
|
@@ -97,6 +107,9 @@ export const AuthContextProvider = ({ | |||||
|
||||||
try { | ||||||
const user = await account.get(); | ||||||
if (user.labels.includes('admin')) { | ||||||
setIsSuperAdmin(true); | ||||||
} | ||||||
choir241 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const userData: IUser = await getCurrentUser(user.$id); | ||||||
updateUser(userData.id, userData.email, userData.leagues); | ||||||
return userData; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the return removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ it is back in :). Not sure why it was removed. |
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) Gridiron Survivor. | ||
// Licensed under the MIT License. | ||
|
||
export const adminRoutes: string[] = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't this be a wildcard? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ done |
||
'/admin', | ||
'/admin/leagues', | ||
'/admin/players', | ||
'/admin/notifications', | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe this needs to be a useState. It's going to cause a rehydration of the entire application that may be alreayd handled from other auth functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ done.