mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 02:33:01 +00:00
updated JWT to include sessionId and added cookie helpers
This commit is contained in:
@@ -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("=")];
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user