Skip to content
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

Merged
merged 15 commits into from
Oct 23, 2024

Conversation

chris-nowicki
Copy link
Contributor

@chris-nowicki chris-nowicki commented Aug 7, 2024

fixes #381

Added access for SUPER ADMINS only. This has to be a field added in the database manually under the user > labels data field. This should only apply to GIS Developers.

VIDEO

CleanShot.2024-10-16.at.23.58.17.mp4

Copy link

appwrite bot commented Aug 7, 2024

Gridiron Survivor Application 6616ea581ef9f5521c7d

Function ID Status Action
Your function has been successfully deployed.

Project name: Gridiron Survivor Application
Project ID: 6616ea581ef9f5521c7d

Function ID Status Action
userAuth 6626fef885a9f630442b ready Ready View Logs

Only deployments on the production branch are activated automatically. If you'd like to activate this deployment, navigate to your deployments. Learn more about Appwrite Function deployments.

💡 Did you know?
Cursor pagination performs better than offset pagination when loading further pages

Copy link

vercel bot commented Aug 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
gridiron-survivor ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 23, 2024 5:59pm
gridiron-survivor-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 23, 2024 5:59pm

context/AuthContextProvider.tsx Outdated Show resolved Hide resolved
context/AuthContextProvider.tsx Outdated Show resolved Hide resolved
ryandotfurrer
ryandotfurrer previously approved these changes Aug 9, 2024
choir241
choir241 previously approved these changes Aug 9, 2024
Copy link
Collaborator

@shashilo shashilo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chris-nowicki Can you show me an example of this working in action?

@@ -40,6 +41,7 @@ export const AuthContextProvider = ({
children: React.ReactNode;
}): JSX.Element => {
const [isSignedIn, setIsSignedIn] = useState<boolean>(false);
const [isSuperAdmin, setIsSuperAdmin] = useState<boolean>(false);
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ done.

// Copyright (c) Gridiron Survivor.
// Licensed under the MIT License.

export const adminRoutes: string[] = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't this be a wildcard? admin/*? Then, we don't need to track every page.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ done

useMemo(() => {
if (isSignedIn) {
if (adminRoutes.includes(pathname)) {
!isSuperAdmin && router.push('/league/all');
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
!isSuperAdmin && router.push('/league/all');
!isSuperAdmin && router.push('/');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shashilo tried this out and nope I am not redirected automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ✅

@chris-nowicki chris-nowicki changed the title feat(#381): Implement User Role Access chris/feat(#381): Implement User Role Access Aug 17, 2024
if (pathname.startsWith('/admin')) {
!user.labels.includes('admin') && router.push('/');
}
}, [user, pathname]);

Copy link
Contributor

@vmaineng vmaineng Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we write out other edge cases such as

  1. if user.labels is undefined or not yet populated
  2. if the logic has multiple stated updates (i.e. user or pathname changing frequently causing multiple redirects (to prevent adding to the history stack as a user can go forward and backward)

What are your thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The way the useEffect is written user will be populated if it hits this point
    if (user.id === '' || user.email === '') {
      getUser();
      return;
    }
    ```

2. This is a question I'd like to defer to @shashilo ... right now this is the only way I was able to get it to work due when we change pathname it loads the data again (using cached data) but we still need to wait for it to load.    
    

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

if (pathname.startsWith('/admin')) {
!user.labels.includes('admin') && router.push('/');
}
}, [user, pathname]);

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

@@ -89,9 +93,8 @@ export const AuthContextProvider = ({

try {
const user = await account.get();
const userData: IUser = await getCurrentUser(user.$id);
updateUser(userData.id, userData.email, userData.leagues);
return userData;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the return removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ it is back in :). Not sure why it was removed.

pnpm-lock.yaml Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the lock file updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ fixed!

Copy link
Contributor

@vazquezea96 vazquezea96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work Chris!

@chris-nowicki chris-nowicki merged commit a267371 into develop Oct 23, 2024
5 checks passed
@chris-nowicki chris-nowicki deleted the chris/add-admin-user-permissions branch October 23, 2024 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FEE: Implement User Role Access
6 participants