mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
4466ba436b
## Description This PR ensures that the default prettier config is used for examples and templates. This config is compatible with `prettier@3` as well (upgrading prettier is bigger change that can be a future PR). ## Changes - Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will be needed upgrading to prettier@3) - Added `examples/.prettierrc.json` with default config (this will change every example) - Added `packages/create-next-app/templates/.prettierrc.json` with default config (this will change every template) ## Related - Fixes #54402 - Closes #54409
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
import { parse, serialize } from "cookie";
|
|
import { createLoginSession, getLoginSession } from "./auth";
|
|
|
|
function parseCookies(req) {
|
|
// For API Routes we don't need to parse the cookies.
|
|
if (req.cookies) return req.cookies;
|
|
|
|
// For pages we do need to parse the cookies.
|
|
const cookie = req.headers?.cookie;
|
|
return parse(cookie || "");
|
|
}
|
|
|
|
export default function session({ name, secret, cookie: cookieOpts }) {
|
|
return async (req, res, next) => {
|
|
const cookies = parseCookies(req);
|
|
const token = cookies[name];
|
|
let unsealed = {};
|
|
|
|
if (token) {
|
|
try {
|
|
// the cookie needs to be unsealed using the password `secret`
|
|
unsealed = await getLoginSession(token, secret);
|
|
} catch (e) {
|
|
// The cookie is invalid
|
|
}
|
|
}
|
|
|
|
req.session = unsealed;
|
|
|
|
// We are proxying res.end to commit the session cookie
|
|
const oldEnd = res.end;
|
|
res.end = async function resEndProxy(...args) {
|
|
if (res.finished || res.writableEnded || res.headersSent) return;
|
|
if (cookieOpts.maxAge) {
|
|
req.session.maxAge = cookieOpts.maxAge;
|
|
}
|
|
|
|
const token = await createLoginSession(req.session, secret);
|
|
|
|
res.setHeader("Set-Cookie", serialize(name, token, cookieOpts));
|
|
oldEnd.apply(this, args);
|
|
};
|
|
|
|
next();
|
|
};
|
|
}
|