mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-07 14:16:01 +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>,
|
middleware: (request: NextAuthRequest) => Promise<NextResponse>,
|
||||||
) => {
|
) => {
|
||||||
return async (request: NextAuthRequest) => {
|
return async (request: NextAuthRequest) => {
|
||||||
let legacySession: Session | null = null;
|
let session: Session | null = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
legacySession = await getLegacySession();
|
session = await auth();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
let session = legacySession;
|
let isLegacySession = false;
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
try {
|
try {
|
||||||
session = await auth();
|
session = await getLegacySession();
|
||||||
|
if (session) {
|
||||||
|
isLegacySession = true;
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
@ -50,7 +53,7 @@ export const withAuth = (
|
||||||
|
|
||||||
const middlewareRes = await middleware(request);
|
const middlewareRes = await middleware(request);
|
||||||
|
|
||||||
if (legacySession) {
|
if (isLegacySession) {
|
||||||
try {
|
try {
|
||||||
await migrateLegacyJWT(middlewareRes);
|
await migrateLegacyJWT(middlewareRes);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -58,6 +58,7 @@ export async function migrateLegacyJWT(res: NextResponse) {
|
||||||
salt: newCookieName,
|
salt: newCookieName,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Set new session cookie
|
||||||
res.cookies.set(newCookieName, newJWT, {
|
res.cookies.set(newCookieName, newJWT, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: isSecureCookie,
|
secure: isSecureCookie,
|
||||||
|
@ -65,6 +66,14 @@ export async function migrateLegacyJWT(res: NextResponse) {
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
path: "/",
|
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 auth = async () => {
|
||||||
const session = await getLegacySession();
|
const session = await originalAuth();
|
||||||
if (session) {
|
if (session) {
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
return originalAuth();
|
return getLegacySession();
|
||||||
};
|
};
|
||||||
|
|
||||||
export { auth, handlers, signIn, signOut };
|
export { auth, handlers, signIn, signOut };
|
||||||
|
|
Loading…
Add table
Reference in a new issue