-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
withMiddlewareAuthRequired doesn't check if access token has expired #1723
Comments
Had to add custom code to check for it. Isn't there a better way to do that?
|
Out of curiosity, do you also get the following error message:
|
ye but shouldnt be related |
@jln13x – I implemented the same logic as your code, and it worked until I received the AccessTokenErrorCode I solved it by updating the session to expire. This may not be the right way to do it (I know the documentation recommend against it), but I redirect to the logout URL afterwards to clean up the session: import {
AccessTokenError,
getAccessToken,
updateSession,
withMiddlewareAuthRequired,
} from "@auth0/nextjs-auth0/edge";
import { NextResponse } from "next/server";
export default withMiddlewareAuthRequired({
middleware: async function middleware(req) {
try {
await getAccessToken();
return NextResponse.next();
} catch (err) {
if (err instanceof AccessTokenError) {
const res = NextResponse.redirect(
"https://www.example.com/api/auth/logout",
);
return updateSession(req, res, {
user: [],
accessToken: undefined,
idToken: undefined,
refreshToken: undefined,
accessTokenExpiresAt: 0,
});
}
/* Fallback: if you don't know how to handle the error */
throw err;
}
},
}); |
Checklist
Description
The
withMiddlewareAuthRequired
doesn't seem to check if the access token is actually still validReproduction
Additional context
No response
nextjs-auth0 version
3.5.0
Next.js version
14.1.3
Node.js version
v20.11.1
The text was updated successfully, but these errors were encountered: