CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /**
* Authentication utilities — password hashing, session tokens.
* Uses Bun's native crypto for Argon2-like password hashing.
*/
const SESSION_DURATION_MS = 30 * 24 * 60 * 60 * 1000; // 30 days
export async function hashPassword(password: string): Promise<string> {
return await Bun.password.hash(password, { algorithm: "bcrypt", cost: 10 });
}
export async function verifyPassword(
password: string,
hash: string
): Promise<boolean> {
return await Bun.password.verify(password, hash);
}
export function generateSessionToken(): string {
const bytes = crypto.getRandomValues(new Uint8Array(32));
return Array.from(bytes)
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
export function sessionCookieOptions(): {
httpOnly: boolean;
secure: boolean;
sameSite: "Lax";
path: string;
maxAge: number;
} {
return {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "Lax",
path: "/",
maxAge: SESSION_DURATION_MS / 1000,
};
}
export function sessionExpiry(): Date {
return new Date(Date.now() + SESSION_DURATION_MS);
}
|