|
1 |
| -import type { NextRequest } from "next/server"; |
2 |
| -import { NextResponse } from "next/server"; |
| 1 | +import { absoluteUrl } from "@rallly/utils/absolute-url"; |
| 2 | +import { cookies } from "next/headers"; |
| 3 | +import type { NextResponse } from "next/server"; |
| 4 | +import type { Session } from "next-auth"; |
3 | 5 | import { encode } from "next-auth/jwt";
|
4 | 6 |
|
5 | 7 | import { decodeLegacyJWT } from "./helpers/jwt";
|
6 | 8 |
|
7 |
| -const isSecureCookie = |
8 |
| - process.env.NEXT_PUBLIC_BASE_URL?.startsWith("https://") ?? false; |
| 9 | +const isSecureCookie = absoluteUrl().startsWith("https://"); |
9 | 10 |
|
10 | 11 | const prefix = isSecureCookie ? "__Secure-" : "";
|
11 | 12 |
|
12 | 13 | const oldCookieName = prefix + "next-auth.session-token";
|
13 | 14 | const newCookieName = prefix + "authjs.session-token";
|
14 | 15 |
|
15 |
| -/** |
16 |
| - * Migrates the next-auth cookies to the new authjs cookie names |
17 |
| - * This is needed for next-auth v5 which renamed the cookie prefix from 'next-auth' to 'authjs' |
18 |
| - */ |
19 |
| -export function withAuthMigration( |
20 |
| - middleware: (req: NextRequest) => void | Response | Promise<void | Response>, |
21 |
| -) { |
22 |
| - return async (req: NextRequest) => { |
23 |
| - const oldCookie = req.cookies.get(oldCookieName); |
| 16 | +export async function getLegacySession(): Promise<Session | null> { |
| 17 | + const cookieStore = cookies(); |
| 18 | + const legacySessionCookie = cookieStore.get(oldCookieName); |
| 19 | + if (legacySessionCookie) { |
| 20 | + const decodedCookie = await decodeLegacyJWT(legacySessionCookie.value); |
24 | 21 |
|
25 |
| - // If the old cookie doesn't exist, return the middleware |
26 |
| - if (!oldCookie) { |
27 |
| - return middleware(req); |
| 22 | + if (decodedCookie?.sub) { |
| 23 | + const { sub: id, ...rest } = decodedCookie; |
| 24 | + return { |
| 25 | + user: { id, ...rest }, |
| 26 | + expires: decodedCookie.exp |
| 27 | + ? new Date(decodedCookie.exp * 1000).toISOString() |
| 28 | + : new Date(Date.now() + 30 * 60 * 60 * 1000).toISOString(), |
| 29 | + }; |
28 | 30 | }
|
| 31 | + } |
29 | 32 |
|
30 |
| - const response = NextResponse.redirect(req.url); |
31 |
| - response.cookies.delete(oldCookieName); |
| 33 | + return null; |
| 34 | +} |
32 | 35 |
|
33 |
| - // If the new cookie exists, delete the old cookie first and rerun middleware |
34 |
| - if (req.cookies.get(newCookieName)) { |
35 |
| - return response; |
| 36 | +async function getLegacyJWT() { |
| 37 | + const cookieStore = cookies(); |
| 38 | + const legacySessionCookie = cookieStore.get(oldCookieName); |
| 39 | + if (legacySessionCookie) { |
| 40 | + const decodedCookie = await decodeLegacyJWT(legacySessionCookie.value); |
| 41 | + if (decodedCookie) { |
| 42 | + return decodedCookie; |
36 | 43 | }
|
| 44 | + } |
| 45 | + return null; |
| 46 | +} |
37 | 47 |
|
38 |
| - const decodedCookie = await decodeLegacyJWT(oldCookie.value); |
39 |
| - |
40 |
| - // If old cookie is invalid, delete the old cookie first and rerun middleware |
41 |
| - if (!decodedCookie) { |
42 |
| - return response; |
43 |
| - } |
| 48 | +/** |
| 49 | + * Replace the old legacy cookie with the new one |
| 50 | + */ |
| 51 | +export async function migrateLegacyJWT(res: NextResponse) { |
| 52 | + const legacyJWT = await getLegacyJWT(); |
44 | 53 |
|
45 |
| - // Set the new cookie |
46 |
| - const encodedCookie = await encode({ |
47 |
| - token: decodedCookie, |
| 54 | + if (legacyJWT) { |
| 55 | + const newJWT = await encode({ |
| 56 | + token: legacyJWT, |
48 | 57 | secret: process.env.SECRET_PASSWORD,
|
49 | 58 | salt: newCookieName,
|
50 | 59 | });
|
51 | 60 |
|
52 |
| - // Set the new cookie with the same value and attributes |
53 |
| - response.cookies.set(newCookieName, encodedCookie, { |
54 |
| - path: "/", |
| 61 | + res.cookies.set(newCookieName, newJWT, { |
| 62 | + httpOnly: true, |
55 | 63 | secure: isSecureCookie,
|
| 64 | + expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7), |
56 | 65 | sameSite: "lax",
|
57 |
| - httpOnly: true, |
| 66 | + path: "/", |
58 | 67 | });
|
59 |
| - |
60 |
| - // Delete the old cookie |
61 |
| - response.cookies.delete(oldCookieName); |
62 |
| - |
63 |
| - return response; |
64 |
| - }; |
| 68 | + res.cookies.delete(oldCookieName); |
| 69 | + } |
65 | 70 | }
|
0 commit comments