#Token expires

3 messages · Page 1 of 1 (latest)

hidden tinsel
#

I'm using @convex-dev/auth (github oAuth) in my next js app,
I noticed that after sometime the token expires, however, the middleware is not kicking the user out, thus the page request is proceeding and getting error in response from the server.

following is the middleware code

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

const isPublicPage = createRouteMatcher(["/"]);


export default convexAuthNextjsMiddleware((request) => {

  if (!isPublicPage(request) && !isAuthenticatedNextjs()) {
    return nextjsMiddlewareRedirect(request, "/");
  }
  if (isPublicPage(request) && isAuthenticatedNextjs()) {
    return nextjsMiddlewareRedirect(request, "/modules");
  }
});

export const config = {
  // The following matcher runs middleware on all routes
  // except static assets.
  matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
tranquil bearBOT
#

Hi @hidden tinsel. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

It seems like you're experiencing an issue where the JWT token expires but the user is not logged out, causing errors when making requests to the server. This could be due to the isAuthenticatedNextjs() function, which only checks if there is an auth token cookie present, but does not verify if the token is valid or expired source (https://discord.com/channels/1019350475847499849/1279823393125961748).

One way to handle this is to manually refresh the JWT token before it expires. However, it appears that the Convex Auth library does not automatically refresh the JWT token source (https://discord.com/channels/1019350475847499849/1270776251732201522).

In the meantime, you could manually clear the cookies when you encounter this issue, as suggested by a community member in the Discord thread source (https://discord.com/channels/1019350475847499849/1279823393125961748).

Please note that Convex Auth + Next.js SSA support is still experimental, so there might be some bugs and issues that need to be addressed source (https://discord.com/channels/1019350475847499849/1279823393125961748).

For more information on how to set up authentication in Next.js with Convex, you can refer to the official documentation (https://labs.convex.dev/auth/authz/nextjs).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: