Skip to content

Commit

Permalink
Remove unused
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella committed Feb 12, 2025
1 parent a7a55e2 commit 72d84c9
Showing 1 changed file with 0 additions and 64 deletions.
64 changes: 0 additions & 64 deletions apps/web/src/auth/legacy/next-auth-cookie-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,67 +66,3 @@ export async function migrateLegacyJWT(res: NextResponse) {
res.cookies.delete(oldCookieName);
}
}

/**
* Migrates the next-auth cookies to the new authjs cookie names
* This is needed for next-auth v5 which renamed the cookie prefix from 'next-auth' to 'authjs'
*/
export function withAuthMigration(
middleware: (req: NextRequest) => Promise<NextResponse>,
) {
async function runMiddlewareAndDeleteOldCookie(req: NextRequest) {
const res = await middleware(req);
res.cookies.set(oldCookieName, "", {
httpOnly: true,
secure: isSecureCookie,
sameSite: "lax",
path: "/",
});
return res;
}
return async (req: NextRequest) => {
const oldCookie = req.cookies.get(oldCookieName);

if (req.cookies.get(newCookieName) || !oldCookie || !oldCookie.value) {
// exit early if the new cookie exists or the old cookie doesn't exist or is invalid
return middleware(req);
}

try {
const decodedCookie = await decodeLegacyJWT(oldCookie.value);

// If old cookie is invalid, delete the old cookie
if (decodedCookie) {
// Set the new cookie
const encodedCookie = await encode({
token: decodedCookie,
secret: process.env.SECRET_PASSWORD,
salt: newCookieName,
});

// Run the middleware with the new cookie set
req.cookies.set({
name: newCookieName,
value: encodedCookie,
});

const res = await runMiddlewareAndDeleteOldCookie(req);

// Set the new cookie in the response
res.cookies.set(newCookieName, encodedCookie, {
httpOnly: true,
secure: isSecureCookie,
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7),
sameSite: "lax",
path: "/",
});

return res;
}
} catch (e) {
console.error(e);
}

return runMiddlewareAndDeleteOldCookie(req);
};
}

0 comments on commit 72d84c9

Please sign in to comment.