mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-05 10:37:30 +02:00
Update
This commit is contained in:
parent
1d4cd748c7
commit
bd6c803a4b
1 changed files with 36 additions and 24 deletions
|
@ -2,6 +2,7 @@ import { absoluteUrl } from "@rallly/utils/absolute-url";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import type { NextResponse } from "next/server";
|
import type { NextResponse } from "next/server";
|
||||||
import type { Session } from "next-auth";
|
import type { Session } from "next-auth";
|
||||||
|
import type { JWT } from "next-auth/jwt";
|
||||||
import { encode } from "next-auth/jwt";
|
import { encode } from "next-auth/jwt";
|
||||||
|
|
||||||
import { decodeLegacyJWT } from "./helpers/jwt";
|
import { decodeLegacyJWT } from "./helpers/jwt";
|
||||||
|
@ -16,7 +17,7 @@ const newCookieName = prefix + "authjs.session-token";
|
||||||
export async function getLegacySession(): Promise<Session | null> {
|
export async function getLegacySession(): Promise<Session | null> {
|
||||||
const cookieStore = cookies();
|
const cookieStore = cookies();
|
||||||
const legacySessionCookie = cookieStore.get(oldCookieName);
|
const legacySessionCookie = cookieStore.get(oldCookieName);
|
||||||
if (legacySessionCookie) {
|
if (legacySessionCookie && legacySessionCookie.value) {
|
||||||
const decodedCookie = await decodeLegacyJWT(legacySessionCookie.value);
|
const decodedCookie = await decodeLegacyJWT(legacySessionCookie.value);
|
||||||
|
|
||||||
if (decodedCookie?.sub) {
|
if (decodedCookie?.sub) {
|
||||||
|
@ -45,30 +46,12 @@ async function getLegacyJWT() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function deleteLegacyCookie(res: NextResponse) {
|
||||||
* Replace the old legacy cookie with the new one
|
const cookieStore = cookies();
|
||||||
*/
|
const oldCookie = cookieStore.get(oldCookieName);
|
||||||
export async function migrateLegacyJWT(res: NextResponse) {
|
if (oldCookie) {
|
||||||
const legacyJWT = await getLegacyJWT();
|
|
||||||
|
|
||||||
if (legacyJWT) {
|
|
||||||
const newJWT = await encode({
|
|
||||||
token: legacyJWT,
|
|
||||||
secret: process.env.SECRET_PASSWORD,
|
|
||||||
salt: newCookieName,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Set new session cookie
|
|
||||||
res.cookies.set(newCookieName, newJWT, {
|
|
||||||
httpOnly: true,
|
|
||||||
secure: isSecureCookie,
|
|
||||||
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7),
|
|
||||||
sameSite: "lax",
|
|
||||||
path: "/",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Delete the old cookie
|
// Delete the old cookie
|
||||||
res.cookies.set(oldCookieName, "", {
|
res.cookies.set(oldCookieName, oldCookie.value, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: isSecureCookie,
|
secure: isSecureCookie,
|
||||||
expires: new Date(0),
|
expires: new Date(0),
|
||||||
|
@ -77,3 +60,32 @@ export async function migrateLegacyJWT(res: NextResponse) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function setNewSessionCookie(res: NextResponse, jwt: JWT) {
|
||||||
|
const newJWT = await encode({
|
||||||
|
token: jwt,
|
||||||
|
secret: process.env.SECRET_PASSWORD,
|
||||||
|
salt: newCookieName,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Set new session cookie
|
||||||
|
res.cookies.set(newCookieName, newJWT, {
|
||||||
|
httpOnly: true,
|
||||||
|
secure: isSecureCookie,
|
||||||
|
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7),
|
||||||
|
sameSite: "lax",
|
||||||
|
path: "/",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace the old legacy cookie with the new one
|
||||||
|
*/
|
||||||
|
export async function migrateLegacyJWT(res: NextResponse) {
|
||||||
|
const legacyJWT = await getLegacyJWT();
|
||||||
|
|
||||||
|
if (legacyJWT) {
|
||||||
|
await setNewSessionCookie(res, legacyJWT);
|
||||||
|
deleteLegacyCookie(res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue