Skip to content

Commit

Permalink
🐛 Only migrate legacy token if nextauth token doesn’t exist
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella committed Nov 3, 2023
1 parent 1e93a4f commit 25da819
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions apps/web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,30 @@ export default withAuth(

const res = NextResponse.rewrite(newUrl);

/**
* We moved from a bespoke session implementation to next-auth.
* This middleware looks for the old session cookie and moves it to
* a temporary cookie accessible to the client which will exchange it
* for a new session token with the legacy-token provider.
*/
const legacyToken = req.cookies.get("rallly-session");
if (legacyToken) {
// delete old cookie
res.cookies.delete("rallly-session");
// make sure old cookie isn't expired
const payload = await unsealData(legacyToken.value, {
password: process.env.SECRET_PASSWORD,
});
// if it's not expired, write it to a new cookie that we
// can read from the client
if (Object.keys(payload).length > 0) {
res.cookies.set({
name: "legacy-token",
value: legacyToken.value,
if (!req.nextauth.token) {
/**
* We moved from a bespoke session implementation to next-auth.
* This middleware looks for the old session cookie and moves it to
* a temporary cookie accessible to the client which will exchange it
* for a new session token with the legacy-token provider.
*/
const legacyToken = req.cookies.get("rallly-session");
if (legacyToken) {
// delete old cookie
res.cookies.delete("rallly-session");
// make sure old cookie isn't expired
const payload = await unsealData(legacyToken.value, {
password: process.env.SECRET_PASSWORD,
});
// if it's not expired, write it to a new cookie that we
// can read from the client
if (Object.keys(payload).length > 0) {
res.cookies.set({
name: "legacy-token",
value: legacyToken.value,
httpOnly: false,
});
}
}
}

Expand Down

1 comment on commit 25da819

@vercel
Copy link

@vercel vercel bot commented on 25da819 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

app – ./

rallly-vert.vercel.app
app-rallly.vercel.app
app-git-main-rallly.vercel.app
app.rallly.co

Please sign in to comment.