updated JWT to include sessionId and added cookie helpers

This commit is contained in:
Oliver Bryan
2026-01-09 05:32:37 +00:00
parent 6884cc1c8e
commit 0ec2ec3f2a

View File

@@ -2,7 +2,8 @@ import bcrypt from "bcrypt";
import * as jwt from "jsonwebtoken";
const JWT_EXPIRES_IN = (process.env.JWT_EXPIRES_IN ?? "7d") as jwt.SignOptions["expiresIn"];
const JWT_ALGORITHM = "HS256";
const JWT_ALGORITHM = "HS256" as const;
const COOKIE_MAX_AGE = 7 * 24 * 60 * 60; // 7 days in seconds
const requireJwtSecret = () => {
const secret = process.env.JWT_SECRET;
@@ -19,9 +20,9 @@ export const hashPassword = (password: string) => bcrypt.hash(password, 10);
export const verifyPassword = (password: string, hash: string) => bcrypt.compare(password, hash);
export const generateToken = (userId: number) => {
export const generateToken = (sessionId: number, userId: number) => {
const secret = requireJwtSecret();
return jwt.sign({ userId }, secret, {
return jwt.sign({ sessionId, userId }, secret, {
expiresIn: JWT_EXPIRES_IN,
algorithm: JWT_ALGORITHM,
});
@@ -31,5 +32,23 @@ export const verifyToken = (token: string) => {
const secret = requireJwtSecret();
return jwt.verify(token, secret, {
algorithms: [JWT_ALGORITHM],
}) as { userId: number };
}) as { sessionId: number; userId: number };
};
export const buildAuthCookie = (token: string) => {
return `token=${token}; HttpOnly; Secure; SameSite=None; Path=/; Max-Age=${COOKIE_MAX_AGE}`; // it pains me that this is in seconds
};
export const buildClearAuthCookie = () => {
return "token=; HttpOnly; Secure; SameSite=None; Path=/; Max-Age=0";
};
export const parseCookies = (cookieHeader: string | null): Record<string, string> => {
if (!cookieHeader) return {};
return Object.fromEntries(
cookieHeader.split(";").map((cookie) => {
const [key, ...rest] = cookie.trim().split("=");
return [key, rest.join("=")];
}),
);
};