🐛 Fix infinite loop when trying to migrate legacy cookie (#1561)

This commit is contained in:
Luke Vella 2025-02-13 10:14:03 +07:00 committed by GitHub
parent cb27ae9ea7
commit ff4a1d16cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 260 additions and 139 deletions

View file

@ -0,0 +1,63 @@
import type { NextResponse } from "next/server";
import type { NextAuthRequest, Session } from "next-auth";
import NextAuth from "next-auth";
import { nextAuthConfig } from "@/next-auth.config";
import {
getLegacySession,
migrateLegacyJWT,
} from "../legacy/next-auth-cookie-migration";
const { auth } = NextAuth(nextAuthConfig);
export const withAuth = (
middleware: (request: NextAuthRequest) => Promise<NextResponse>,
) => {
return async (request: NextAuthRequest) => {
let legacySession: Session | null = null;
try {
legacySession = await getLegacySession();
} catch (e) {
console.error(e);
}
let session = legacySession;
if (!session) {
try {
session = await auth();
} catch (e) {
console.error(e);
}
}
try {
const res = await nextAuthConfig.callbacks.authorized({
request,
auth: session,
});
if (res !== true) {
return res;
}
} catch (e) {
console.error(e);
}
request.auth = session;
const middlewareRes = await middleware(request);
if (legacySession) {
try {
await migrateLegacyJWT(middlewareRes);
} catch (e) {
console.error(e);
}
}
return middlewareRes;
};
};