Jacob KimJ
Convex Community2y ago
9 replies
Jacob Kim

how to refresh JWTs before they expire

The default jwt.durationMs is an hour and it seems like convex-auth does not automatically refresh jwt. Am I responsible for refreshing the JWT before it expires? Or did I misconfigure something? I am using Next.js, and I am handling auth stuff with middleware like below.

import {
  convexAuthNextjsMiddleware,
  createRouteMatcher,
  isAuthenticatedNextjs,
  nextjsMiddlewareRedirect,
  convexAuthNextjsToken,
} from '@convex-dev/auth/nextjs/server'

const isSignInPage = createRouteMatcher(['/login'])
const isProtectedRoute = createRouteMatcher(['/app(.*)'])

export default convexAuthNextjsMiddleware((request) => {
  if (isSignInPage(request) && isAuthenticatedNextjs()) {
    return nextjsMiddlewareRedirect(request, '/app')
  }

  if (isProtectedRoute(request) && !isAuthenticatedNextjs()) {
    return nextjsMiddlewareRedirect(request, '/login')
  }
})

export const config = {
  // The following matcher runs middleware on all routes
  // except static assets.
  matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api)(.*)'],
}
Was this page helpful?