Welcome to the Kinde community.

A
A
A
M
T

Issues with getting roles for users in NextJS App Router

Hello Team. I am having issues getting roles for users. I have setup the roles and the permissions.

I am using NextJS App Router and everything works exactly perfectly. When i call getUser i have all details and getPermissions but getRoles returns null even though roles are setup

C
T
O
21 comments

Hey there. Let me look into this for you. Can you provide your Kinde business name?

Hi here's a few troubleshooting tips:

1. Role Configuration: Ensure that the roles are correctly configured in your Kinde account. Double-check that the roles are assigned to the user you are testing with.

2. API Call: Verify that the API call to fetch roles is correctly implemented. Sometimes, the issue might be with how the data is being fetched or processed.

3. Session Data: Make sure that the session data includes roles. You can log the session data to see if roles are being included.

Here is a basic example of how to fetch roles using getKindeServerSession:

import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

export default async function Home() {
  const { getRoles } = getKindeServerSession();
  const roles = await getRoles();

  console.log(roles); // This should log the roles if they are correctly set up

  return (
    <div>
      <h1>User Roles</h1>
      <pre>{JSON.stringify(roles, null, 2)}</pre>
    </div>
  );
}
Let me know if this helps or if you need further assistance.

The code sample is for Next.js. Let us know if you need an example for a different framework.

yup this was exactly what i did

i also called getPermissions as there are permissions assigned to the role

the permissions come back perfectly but not the roles

  user: publicProcedure.query(async () => {
    const { getUser, isAuthenticated, getPermissions, getRoles, getOrganization } =
      getKindeServerSession();

    const { user, roles, permissions, organization, authenticated } = await props({
      user: getUser(),
      roles: getRoles(),
      permissions: getPermissions(),
      organization: getOrganization(),
      authenticated: isAuthenticated()
    });

    const tier = getTierNameFromRoles(permissions?.permissions ?? []);
    return { tier, user, roles, authenticated, permissions, organization };
  })
};

in this example roles is always null

Hi there. Not to worry, we can look into this for you. Can you tell me your kinde business / kinde domain?

Hey ,
We are still looking into your issue.

Also i don't know if this is related but i am connecting to the management API using the typescript sdk and when i get a user the default organization is not even set on them

and im guessing roles are set on the organization

Hey ,
You are right. Roles and permissions are unique per user.

Are you would check that in your Kinde Settings > Application > Tokens >AccessToken > Roles are switched on?

Also i don't know if this is related but i am connecting to the management API using the typescript sdk and when i get a user the default organization is not even set on them
Is the Add users to the default organization if no organization is specified toggle (under Settings > Environment > Details) turned on?

yes the toggle is on to add to default org

Are you would check that in your Kinde Settings > Application > Tokens >AccessToken > Roles are switched on?
haaaa it looks like the Toggles in settings were not switched on

Hey ,
Thanks for getting back to me.

After enabling the toggle to add roles to the access token, is getRoles still returning null?

getRoles is working now as well thank you

Great to hear.
Thanks for getting back to me

Add a reply
Sign up and join the conversation on Slack
Join