feat: initialize VitePress CMS
This commit is contained in:
59
server/session.mjs
Normal file
59
server/session.mjs
Normal file
@@ -0,0 +1,59 @@
|
||||
import { randomBytes } from "node:crypto";
|
||||
|
||||
const sessions = new Map();
|
||||
|
||||
export function createSession(data) {
|
||||
const id = randomBytes(24).toString("hex");
|
||||
sessions.set(id, {
|
||||
...data,
|
||||
createdAt: Date.now(),
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
export function getSession(id) {
|
||||
if (!id) return undefined;
|
||||
return sessions.get(id);
|
||||
}
|
||||
|
||||
export function updateSession(id, data) {
|
||||
if (!id || !sessions.has(id)) return undefined;
|
||||
const nextSession = {
|
||||
...sessions.get(id),
|
||||
...data,
|
||||
};
|
||||
sessions.set(id, nextSession);
|
||||
return nextSession;
|
||||
}
|
||||
|
||||
export function deleteSession(id) {
|
||||
if (!id) return;
|
||||
sessions.delete(id);
|
||||
}
|
||||
|
||||
export function parseCookies(cookieHeader = "") {
|
||||
return Object.fromEntries(
|
||||
cookieHeader
|
||||
.split(";")
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean)
|
||||
.map((item) => {
|
||||
const index = item.indexOf("=");
|
||||
return [item.slice(0, index), decodeURIComponent(item.slice(index + 1))];
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function sessionCookie(id) {
|
||||
return [
|
||||
`vpc_session=${encodeURIComponent(id)}`,
|
||||
"Path=/",
|
||||
"HttpOnly",
|
||||
"SameSite=Lax",
|
||||
"Max-Age=2592000",
|
||||
].join("; ");
|
||||
}
|
||||
|
||||
export function clearSessionCookie() {
|
||||
return "vpc_session=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0";
|
||||
}
|
||||
Reference in New Issue
Block a user