79496119

Date: 2025-03-09 15:17:29
Score: 2
Natty:
Report link

I'm facing the same issue. As a workaround, I set a short-lived cookie that I check instead.

Here's a generic version of what I did.

In my action where I update the metadata:

import { auth, clerkClient } from '@clerk/nextjs/server'
import {cookies} from "next/headers";


export const storeMyData = async(myData) => {
  const { userId } = await auth()
  
  if (!userId) {
    return { message: 'No Logged In User' }
  }

  const client = await clerkClient()

  try {
  const res = await client.users.updateUserMetadata(userId, {
      publicMetadata: {
        myField: myData,
      },
    })
    const cookieStore = await cookies();
    cookieStore.set('temp-myField','true',{path: '/', maxAge: 60})

    return { message: res.publicMetadata }
  } catch (err) {
    return { error: 'There was an error updating the user metadata.' }
  }
}

And in my middleware file:

import { clerkMiddleware} from '@clerk/nextjs/server'
import { NextRequest } from 'next/server'
import { cookies } from "next/headers"

export default clerkMiddleware(async (auth, req: NextRequest) => {
  const { sessionClaims } = await auth()
  const cookieStore = await cookies()
  const tempMyField = cookieStore.get('temp-myField')?.value 
  const isMyFieldTrue = tempMyField || sessionClaims?.metadata?.myField

  // ... more code
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): I'm facing the same issue
  • Low reputation (1):
Posted by: Jelani John