Welcome to the Kinde community

Updated 3 months ago

Updated User Permissions and Roles Not Reflecting After API Call

At a glance

A community member updated permissions and roles for a user and hit an API to refresh the user's claims, but the changes did not take effect without the user logging out and back in again. The community members discussed various approaches to force the user to get a new token, such as using the refresh token flow, starting an auth flow without a prompt, or calling a refresh tokens function. They tried implementing the refresh tokens function in a Next.js API route, but encountered issues with it not returning new tokens. The community members continued to investigate the issue, and one member provided an example of how to update the user's name and then refresh the tokens, which seemed to work for now. However, the community is still looking for a more robust solution, and the Kinde team is estimating a timeline of around 3 weeks for a release to address this issue.

Useful resources

Hey Guys i updated some permissions and roles for a user and then hit this api
https://kinde.com/api/docs/#refresh-user-claims-and-invalidate-cache
and got
{
"code": "CLAIMS_REFRESH_SUCCESS",
"message": "Claims successfully refreshed"
}
to see if he gets updated without logging out and in again but it didn't work
need help to do it please

4
E
A
A
33 comments

hi , the refreshed claims will be available after the token is exchanged via the refresh token flow. You need to request the offline scope when starting the auth flow, which will generate a refresh token alongside the access token. You could use the refresh token to generate an updated tokens after the refresh clams.

I see we need to document this flow better.

Hello
my main goal now is to make all users to get new token
cz i added some permissions to them


how can i force user to login again ?
i tried to suspend them but they can still work until they logout

If you didn't start the offline session, another method is starting an auth flow without providing any prompt, this way the user will be redirected to Kinde and back, and will receive the updated token.

In case their auth session expired, they will be asked to authenticate, for the already authenticated session the process only involves several redirects and no UI.


can you please share me documentation about it

because i need to do this on production and i can't make mistakes 😅

this needs time to implement is there any faster method?

easier way i need to kick all users out from the system how to do it ?

Your users already have live access tokens which are not connected to Kinde any longer in any way until the token expired.

You need the app to make a request to start the auth flow from your code. With the refresh tokens this would have happened on the page refresh automatically, but without it implemented I can't see a fast method so far.

Which SDK are you using?

let me chat with the team, so far I can't think of an easy fix

just an update, we are still looking into an alternative solution for you.

Hello
thank you sir

, in the Next.js SDK you can refresh tokens by calling:

const session = await getKindeServerSession();
await session.refreshTokens();
The best I can figure is after you’ve made the broad permission changes, deploy your app with this call in a common route conditionally (not recommended to call this frequently as it adds request overhead).

Does this help for your case?

Hello
gonna try it and get back to you
thanks

Hello
i tried it and its look like not working !

, can you provide some details about what you have tried? Are you calling refreshTokens() from middleware?

actually i'm trying to refresh token for the user once he changes a permission to himself
so basically i'm doing it in a api route

, the call to const newTokens = await refreshTokens(); should return new tokens, can you see if there are new tokens returned? If you are getting null returned can you set the environment variable KINDE_DEBUG_MODE=true and see if there are any errors logged?


KINDE_DEBUG_MODE=trueis already on but no error are being logged in console and it is returning null
newTokens null

hey , if you are calling refreshTokens in the context of a Nextjs page, unfortunately, we can’t update the cookies from there which sucks.

So you might have to put the refreshTokens part into a sever action in which case the newTokens should return new tokens and update the permissions.

If the permissions are not updating then it is most likely a cache invalidation thing on our part!!

Hey
let me try this and get back !


i tried calling refreshTokens from route handler it worked but from server side action it didn't !
and i realised that the bearer returned is the same as the one i already got in my cookie
shouldn't be different?

Hey ,
We are still looking into this issue.

Apologies it's still going on.
We will continue to update you on our progress investigating this issue.


just to keep you in loop

export const updateUserName = async (props: {
  userId: string;
  userName: string;
}) => {
  init();
  await Users.updateUser({
    id: props.userId,
    requestBody: {
      given_name: props.userName
    }
  });

  await getKindeServerSession().refreshTokens();
  revalidatePath('/dashboard', 'page');
};

'use client';

import {useKindeBrowserClient} from '@kinde-oss/kinde-auth-nextjs';
import {updateUserName} from '../../actions/kinde';

export const PersonData = () => {
  const {user} = useKindeBrowserClient();
  const handleSubmit = async (formData: FormData) => {
    const givenName = formData.get('givenName') as string;
    await updateUserName({userId: user.id, userName: givenName});
    // window.location.reload(); (if using browser client)
  };
  return (
    <div>
      <form action={handleSubmit}>
        <label htmlFor="orgName">Organization Name:</label>
        <input
          type="text"
          id="givenName"
          name="givenName"
          defaultValue={user?.given_name}
        />
        <button type="submit">Update user first name</button>
      </form>
      <pre>{JSON.stringify(user, null, 2)}</pre>;
    </div>
  );
};

^ this is an example of a form that should update the user via the api, then refresh to get the updated data.

Only thing to note is that if you’re grabbing the user data from getKindeBrowserClient you will have to do a window.location.reload() to get the updated data

we’re planning on fixing this behaviour in a future release

Thank you we will test it out. Any idea around timeline for the release roadmap wise?

Because its a substantial change - we’re estimating ~3 weeks

Hey
thank for your example it worked for now!

Add a reply
Sign up and join the conversation on Slack