Welcome to the Kinde community.

P
K
A
A
A

Issue with getKindeServerSession().refreshTokens(); in Next.js App Router

I am currently integrating Kinde with a Next.js application using the App Router. In my application, when a user logs in and successfully completes a payment transaction, I would like to set new properties for the user as paid user. However, I am encountering an issue when attempting to refresh the ID token using the method getKindeServerSession().refreshTokens();. It seems I am unable to fetch a new ID token after updating the user's properties.

Could you please provide guidance on how to resolve this issue or recommend an alternative approach for refreshing the ID token in this context?

Thank you!
A
A
l
14 comments
@Abel Trần sorry that you are seeing this issue. What version of the Next.js SDK are you using?

When you say you are unable to fetch a new ID token, are you getting an error or is the token coming back un updated?
@Andre @ Kinde
FYI, I am using:

"next": "14.2.4", "@kinde-oss/kinde-auth-nextjs": "^2.3.8"

I don't see any error when I use await getKindeServerSession().refreshTokens();, but the token does not change.
Thanks for the details.

Able to update to v2.3.10?
When I update to v2.3.10, await getKindeServerSession().refreshTokens(); takes a long time to refresh, but the ID Token does not update.
Ok, thanks, I have passed this along to the team
Thank you! Please let me know if there's any further information or assistance I can provide to help with the investigation.
Hi @Abel Trần , just to confirm, these are user properties added to the id token through token customization, then updated via API?

Let me look into whether we should automatically invalid the cache in this case, in the mean time you can try making a call via the Management API to refresh the claims first using this API method: https://kinde.com/api/docs/#refresh-user-claims-and-invalidate-cache
I updated the user properties via API. After that, I tried calling the refresh token and even refreshing the user claims, but the token hasn't changed.
Here is the my sample code
Attachment
image.png
Hi @Abel Trần ,

It looks like the cache should be updated automatically, so you shouldn't need to call the refresh_claims endpoint. After updating the properties via API, you can then call refreshTokens() to get the updated tokens.

Here is the code I used:
Plain Text
    const { getUser, refreshTokens } = getKindeServerSession();

    const userBefore = await getUser();
    console.log("user properties before", userBefore.properties);

    init();
    const res = await Users.updateUserProperty({
      propertyKey: "userprop",
      userId: userBefore.id,
      value: "new value",
    });
    console.log("update response", res);

    const newTokens = await refreshTokens();
    console.log("updated tokens", newTokens);

    const userAfter = await getUser();
    console.log("user properties after", userAfter.properties);
@leo_kinde
I tried refreshTokens() but my token does not change.
What can I do next?
@Andre @ Kinde @leo_kinde

Can you help me check this again?
I successfully updated the value, but I cannot refresh to get the new values or token.
If I log out and log in again, I will receive the new value / token.
Attachments
image.png
image.png
image.png
@Abel Trần , the null response from refreshTokens() looks like there might be some error happening as it should return the new tokens. Can you set the environment variable KINDE_DEBUG_MODE to true and run it again and have a look to see if there are any errors logged?
The refresh token is null when I use it in the middleware. However, when I use the server action, I can see the token, but it’s still the old one. I manually try to update test_kinde_prop in Kinde, but it seems undefined.
Attachments
image.png
image.png
image.png
Add a reply
Sign up and join the conversation on Discord
Join