mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-21 18:27:53 +02:00
🐛 Avoid setting expired legacy user
This commit is contained in:
parent
1d79f117b3
commit
1e93a4f65b
1 changed files with 15 additions and 5 deletions
|
@ -1,12 +1,13 @@
|
||||||
import languages from "@rallly/languages";
|
import languages from "@rallly/languages";
|
||||||
import languageParser from "accept-language-parser";
|
import languageParser from "accept-language-parser";
|
||||||
|
import { unsealData } from "iron-session/edge";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import withAuth from "next-auth/middleware";
|
import withAuth from "next-auth/middleware";
|
||||||
|
|
||||||
const supportedLocales = Object.keys(languages);
|
const supportedLocales = Object.keys(languages);
|
||||||
|
|
||||||
export default withAuth(
|
export default withAuth(
|
||||||
function middleware(req) {
|
async function middleware(req) {
|
||||||
const { headers, nextUrl } = req;
|
const { headers, nextUrl } = req;
|
||||||
const newUrl = nextUrl.clone();
|
const newUrl = nextUrl.clone();
|
||||||
|
|
||||||
|
@ -49,11 +50,20 @@ export default withAuth(
|
||||||
*/
|
*/
|
||||||
const legacyToken = req.cookies.get("rallly-session");
|
const legacyToken = req.cookies.get("rallly-session");
|
||||||
if (legacyToken) {
|
if (legacyToken) {
|
||||||
res.cookies.set({
|
// delete old cookie
|
||||||
name: "legacy-token",
|
|
||||||
value: legacyToken.value,
|
|
||||||
});
|
|
||||||
res.cookies.delete("rallly-session");
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue