Next.js Discord

Discord Forum

Middleware does not get updates from auth-Object

Unanswered
Dwarf Crocodile posted this in #help-forum
Open in Discord
Dwarf CrocodileOP
I'm using clerk as the authentication provider for my application.
In the middleware.ts I'm checking whether a user has configured data in his/her metadata fields. If this is not the case, the user gets redirect to /connect where the missing data can be added.

export default clerkMiddleware(async (auth: ClerkMiddlewareAuth, req: NextRequest) => {
  const { userId } = await auth();

  if (userId) {
    const { sessionClaims } = await auth();
    const { gameId } = sessionClaims?.metadata as UserMetadata;

    if (!gameId && req.nextUrl.pathname !== '/connect') {
      return NextResponse.redirect(new URL('/connect', req.url));
    }

    if (gameId && req.nextUrl.pathname === '/connect') {
      return NextResponse.redirect(new URL('/dashboard', req.url));
    }
  }

  if (isProtectedRoute(req)) {
    await auth.protect();
  }
});


My problem is, that the user cannot navigate to any other route once the steps in the /connection routes are finished and the gameId is available in Clerk. I already debugged the Middlware and it looks that the user metadata is not updated. Once I refresh the website (with F5) everything is available in the user metadata.

Is there any way to revalidate or rerun the middleware?

0 Replies