Feat/admin auth#44
Conversation
…ole, now users which are set to admin in backend have admin functions
…p as now obsolete, hence moved use auth from page level to the component level
…image and exec routes to use this guard. Tested on postman guard is working and effective
RLee64
left a comment
There was a problem hiding this comment.
Nice work! Great to see it working.
Thinking about this a bit more, could we change this to make frontend hold a 'role' with type
"guest" | "user" | "member" | "admin"
Guest - someone who's not signed in
Member - someone who's signed in, is not an admin, and has paid for the year
Legacy - someone who's signed in, is not an admin, and has not paid for the year
Admin - someone who's signed in and is an admin
Ignore the Legacy role for now and assume all non-admin signed in users are members.
I think backend should should also hold information a bit differently. What we'll want instead is a boolean attribute rather than a string for determining whether a user is an admin. This defaults to being false. Later we'll add a field like hasPaid to distinguish between member and legacy users. These attributes are then used in combination to determine the user role assigned at frontend.
📑 Description
Added admin role to the DB and updated the auth to accommodate it. Tested on the home page, and it is working as intended.
Pushed the auth consumption down to the components. The image component now does that admin check itself, hence the image block component no longer requires a passed prop.
Added the admin route guards for necessary routes. Currently, POST, PUT, and DELETE requests are protected, while GET requests are available to the public. I tested it in Postman, and it is working. Created a similar guard for the frontend, where if they try to access a page that is only for admins, the user gets blocked; however do not know how useful this will be as we are building the admin functions on top of the preexisting user pages, but in future, it could be useful
✏️ Summary of Changes
AuthProvider.tsx— addedadmin: "admin" | "user"toGoogleUserinterfaceAuthContext.tsx— addedadmintoAuthContextTypeand context defaultauthRoutes.ts—/meendpoint now pullsadminfrom MongoDB and attaches it to the responseImageBlock.tsx— removedroleprop, now readsadmindirectly fromuseAuth()imageRoutes.ts—POST /protected withrequireAdminmiddlewareexecutivesRoutes.ts—POST,PUT,DELETEprotected withrequireAdminmiddlewareAdminRoute.tsx— new frontend route guard, redirects non-admins to/adminGuard.ts— new backend middleware, returns401if unauthenticated,403if not admin📸 Screenshots
No video - the visible change is the appearance of the edit button on images when logged in as an admin.
Cookie connect sid of a user

Cookie connect sid of admin

Unauthenticated user trying to access routes

✅ Ready?