mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-06 20:51:48 +02:00
🐛 Fix infinite loop when trying to migrate legacy cookie (#1561)
This commit is contained in:
parent
cb27ae9ea7
commit
ff4a1d16cb
14 changed files with 260 additions and 139 deletions
63
apps/web/src/auth/edge/with-auth.ts
Normal file
63
apps/web/src/auth/edge/with-auth.ts
Normal 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;
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue