Best practice for Admin dashboard?
I want to make an admin special page, but on the client side, I didn't know how to check if the user is admin, how can I do it? Should I check if the id from ctx is admin with getServerSideProps or should I check with trpc query? I want to do something like layout
25 Replies
In the
create-t3-app
starting template there is this part:
I recommend you go over that and follow that to the front end.
If you have more questions lemme know 🖖@robotkutya thank you very much, I would like to ask about handling on the client side
like do I need some kind of provider layout or idk
And it would be amazing if you could give me an example template
You don't check on the client side.
then what should I do? do you have any sample?
not sure that this is best practice, but it's a middleware implementation w/ jwt:
https://discord.com/channels/966627436387266600/1067624837507457034/1068639470896824320
how did u assign role to token? where did you take it from?
I couldn't add a role or isAdmin (boolean type) to session.user object in callbacks. isAdmin or role is constantly returning undefined. It exists in the schema and I define 'user' by default, but it returns undefined no matter what I do
btw Im using credentials provider
did you make the user before adding role
Yes I did
there you have the reason its undefined lol
Nope, it shouldnt have be undefined. I set User as the default value.
if a user exists without a role column, and you add role to the table it goes undefined not default
No, I mean there is a role column and its default value is User.
and you said the user was there before you added the role column
I wasnt mean that
I'm sorry if I expressed it wrong
hard to help much without code to look at. i actually had trouble doing this just based off of docs but you can try doing what i did and just reading through all the nextauth/trpc example reps and searching through past issues for other code examples.
@kivish Hey again, I just found how to handle it
In jwt we get user and return it inside token and then
session takes user from token.user
This method allows us to put exactly what we want inside the JWT
Depending on your database structure
Arr you using role based
Or you have a different table for admin
This is how i guard my page when i do role based adim
First i check if the user is signed in
Second check if user has role admin
Also i modified my session object and included role in my next auth types and session
I totally agree, our problem here was to extend the user in the session
So have you solved it?
yes, I did, thank you for different approach
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View