🐛 Clean up expired jwt (#1628)

This commit is contained in:
Luke Vella 2025-03-10 12:19:45 +00:00 committed by GitHub
parent 701875a158
commit 897cdb4cfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 15 deletions

View file

@ -5,6 +5,7 @@ import NextAuth from "next-auth";
import { nextAuthConfig } from "@/next-auth.config";
import {
deleteLegacyCookie,
getLegacySession,
migrateLegacyJWT,
} from "../legacy/next-auth-cookie-migration";
@ -24,6 +25,7 @@ export const withAuth = (
}
let isLegacySession = false;
let isExpiredLegacySession = false;
if (!session) {
try {
@ -32,7 +34,7 @@ export const withAuth = (
isLegacySession = true;
}
} catch (e) {
console.error(e);
isExpiredLegacySession = true;
}
}
@ -54,6 +56,7 @@ export const withAuth = (
const middlewareRes = await middleware(request);
if (isLegacySession) {
console.warn("Found legacy session, migrating…");
try {
await migrateLegacyJWT(middlewareRes);
} catch (e) {
@ -61,6 +64,11 @@ export const withAuth = (
}
}
if (isExpiredLegacySession) {
console.warn("Found expired legacy session, deleting…");
deleteLegacyCookie(middlewareRes);
}
return middlewareRes;
};
};