mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-28 09:46:39 +02:00
🐛 Check for new session first
This commit is contained in:
parent
327852b444
commit
9f1f5b7f95
3 changed files with 20 additions and 8 deletions
|
@ -15,19 +15,22 @@ export const withAuth = (
|
|||
middleware: (request: NextAuthRequest) => Promise<NextResponse>,
|
||||
) => {
|
||||
return async (request: NextAuthRequest) => {
|
||||
let legacySession: Session | null = null;
|
||||
let session: Session | null = null;
|
||||
|
||||
try {
|
||||
legacySession = await getLegacySession();
|
||||
session = await auth();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
let session = legacySession;
|
||||
let isLegacySession = false;
|
||||
|
||||
if (!session) {
|
||||
try {
|
||||
session = await auth();
|
||||
session = await getLegacySession();
|
||||
if (session) {
|
||||
isLegacySession = true;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
@ -50,7 +53,7 @@ export const withAuth = (
|
|||
|
||||
const middlewareRes = await middleware(request);
|
||||
|
||||
if (legacySession) {
|
||||
if (isLegacySession) {
|
||||
try {
|
||||
await migrateLegacyJWT(middlewareRes);
|
||||
} catch (e) {
|
||||
|
|
|
@ -58,6 +58,7 @@ export async function migrateLegacyJWT(res: NextResponse) {
|
|||
salt: newCookieName,
|
||||
});
|
||||
|
||||
// Set new session cookie
|
||||
res.cookies.set(newCookieName, newJWT, {
|
||||
httpOnly: true,
|
||||
secure: isSecureCookie,
|
||||
|
@ -65,6 +66,14 @@ export async function migrateLegacyJWT(res: NextResponse) {
|
|||
sameSite: "lax",
|
||||
path: "/",
|
||||
});
|
||||
res.cookies.delete(oldCookieName);
|
||||
|
||||
// Delete the old cookie
|
||||
res.cookies.set(oldCookieName, "", {
|
||||
httpOnly: true,
|
||||
secure: isSecureCookie,
|
||||
expires: new Date(0),
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -177,12 +177,12 @@ const {
|
|||
});
|
||||
|
||||
const auth = async () => {
|
||||
const session = await getLegacySession();
|
||||
const session = await originalAuth();
|
||||
if (session) {
|
||||
return session;
|
||||
}
|
||||
|
||||
return originalAuth();
|
||||
return getLegacySession();
|
||||
};
|
||||
|
||||
export { auth, handlers, signIn, signOut };
|
||||
|
|
Loading…
Add table
Reference in a new issue