https://supertokens.com/ logo
Adding roles
s

Sekai

04/19/2023, 9:08 PM
I'm trying to add a admin role to any user when they sign up but for some reason it is not assigning the role. This is the code in the frontend config, where it creates the role and then adds the role
export const frontendConfig = () => {
    return {
        appInfo,
        recipeList: [
            EmailPasswordReact.init({
                onHandleEvent: async (context) => {
                    if (context.action === "SESSION_ALREADY_EXISTS") {
                        // TODO:
                        console.log("Session already exists")
                    } else {
                        if (context.action === "SUCCESS") {
                            if (context.isNewUser) {
                                // TODO: Sign up
                                console.log("User signed up")
                                createRole()
                                addRoleToUser(context.user.id)
                            } else {
                                console.log("User signed in")
                                // TODO: Sign in
                            }
                        }
                    }
                }
            }),
            SessionReact.init(),
        ],
EDIT: now I am getting an error of Initialization not done. Did you forget to call the UserRoles.init or SuperTokens.init functions. and I did in the backend config. when I put it in frontend config it breaks the code so I don't have it.
r

rp

04/20/2023, 6:37 AM
hey @Sekai adding a role is done on the backend. You have added the addRoleToUser on the frontend.
s

Sekai

04/20/2023, 3:20 PM
Hi @rp yes that was the issue . but then how do I add the role when the user signs up? I was looking at the doc and I thought you had to do it like that.
r

rp

04/20/2023, 3:20 PM
yea there are sign up override in which you can add a role
s

Sekai

04/20/2023, 4:35 PM
oh I see I have to override in the backend?
r

rp

04/20/2023, 4:45 PM
Yes
s

Sekai

04/20/2023, 4:46 PM
Ahh ty ty
I tried it and it worked
Only one thing
The children inside InvalidClaimHandler for evaluating the user's role doesn't get reflected in the page
I think I will be fine on this
r

rp

04/20/2023, 5:17 PM
I didn’t understand this
s

Sekai

04/20/2023, 5:32 PM
So I have a page similar to setup in the code in the docs:

https://cdn.discordapp.com/attachments/1098354365221314680/1098662552692596857/image.png

and I have the setup when the user signs up they get the admin role
but after signing up whatever is inside the InvalidClaimHandler is not shown for some reason
even though admin role is added
r

rp

04/20/2023, 5:35 PM
what's shown instead?
s

Sekai

04/20/2023, 5:35 PM
null
r

rp

04/20/2023, 5:38 PM
as in an empty screen?
s

Sekai

04/20/2023, 5:38 PM
oh no
it shows the div>You cannot access this page because you are not an admin.
r

rp

04/20/2023, 5:39 PM
right
how have you added the role on the backend?
can is see the code?
s

Sekai

04/20/2023, 5:40 PM

https://cdn.discordapp.com/attachments/1098354365221314680/1098664395028058232/image.png

r

rp

04/20/2023, 5:41 PM
right. After adding the user role, do
await session.fetchAndSetClaim(UserRoleClaim)
(see in here: https://supertokens.com/docs/userroles/managing-roles-and-users)
s

Sekai

04/20/2023, 5:41 PM
inside that backend config?
r

rp

04/20/2023, 5:42 PM
yea.. right after you do
await UserRoles.addRoleRoUser(..)
.
you can get
session
from
response.session
s

Sekai

04/20/2023, 5:43 PM
okay let me test this
omg it worked!
Thank you!!!!!!!!!!!
w

WillB

04/21/2023, 9:03 PM
The timing on this is great, was just looking for this exact thing. Thanks! 🙂