whoamiW
Convex Community3y ago
6 replies
whoami

Refresh clerk auth token when I switch orgs

I am encountering an issue when attempting to implement row level security with your recommended approach. I've taken the step of feeding the org_id to the gender field in the JWT provided by the clerk. However, I am finding that the token is not refreshing immediately after switching organizations.

In my code, I switch the organization, which prompts the clerk hook to reflect the change:

const { organization: org } = useOrganization({})


As expected, org?.id is passed to the query reactively when I switch organizations. However, I've noticed that the JWT's gender field, where I've assigned the org_id, doesn't appear to refresh to the new organization's ID in sync. This discrepancy is causing my chat list to return as empty.

Here's my query code

const data = useQuery('chats:list', {
    org: org?.id || '',
})


Here is my RLS code

export const { withQueryRLS, withMutationRLS } = RowLevelSecurity<
  { auth: Auth; db: DatabaseReader },
  DataModel
>({
  chats: {
    read: async ({ auth }, chat) => {
      const identity = await auth.getUserIdentity()
      if (!identity) {
        throw new Error('Unauthenticated call to mutation')
      }
      const { gender: org } = identity
      return chat.org === org
    },
...


I need the JWT to refresh immediately after an organization switch to ensure the correct data is being accessed.
Was this page helpful?