Skip to content

refreshSession doesn't update access token and claims #317

@cbix

Description

@cbix

Describe the bug
According to the README, await refreshSession() should result in the session information getting updated if user information like permissions or role changes.

This doesn't seem to work within a single request. My use case is allowing the user to select their preferred language (among other info) and storing this in their metadata object. The nextjs route is invalidated and gets the user info using withAuth and getTokenClaims, however these seem to not use a fresh access token with the updated metadata.

To Reproduce
Make sure the JWT template maps the metadata accordingly, e.g.

{
  "user-key": {
    "locale": {{user.metadata.locale}},
  }
}

Execute the following server action:

export const updateUserProfile = async () => {
  const { user } = await withAuth({ ensureSignedIn: true });
  let claims = await getTokenClaims();
  console.log('token clamis before update', claims);
  await getWorkOS().userManagement.updateUser({
    userId: user.id,
    metadata: { locale: 'de' },
  });
  const refreshResult = await refreshSession();
  console.log('refresh result', refreshResult);
  claims = await getTokenClaims();
  console.log('token claims after update', claims);
};

The tokens before and after the update + refreshSession are identical. Only decoding the access token from refreshResult actually yields the updated information, however I can't access this in a server component as I would do withAuth.

Expected behavior
The logs should print

token claims after update
{
  "user-key": { "locale": "de" }
}

Actual output

token claims before update
{
  "user-key": { "locale": null }
}

token claims after update
{
  "user-key": { "locale": null }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions