UNPKG

@withstudiocms/auth-kit

Version:

Utilities for managing authentication

30 lines (29 loc) 882 B
import { sha256 } from "@oslojs/crypto/sha2"; import { encodeHexLowerCase } from "@oslojs/encoding"; import { Effect, pipe } from "@withstudiocms/effect"; import { useSessionError } from "../errors.js"; const defaultSessionConfig = { expTime: 1e3 * 60 * 60 * 24 * 14, cookieName: "auth_session" }; const makeSessionId = Effect.fn( (token) => useSessionError(() => { if (typeof token !== "string" || token.length === 0) { throw new TypeError("Invalid token"); } return pipe(new TextEncoder().encode(token), sha256, encodeHexLowerCase); }) ); const makeExpirationDate = Effect.fn( (expTime) => useSessionError(() => { if (!Number.isFinite(expTime) || expTime <= 0) { throw new TypeError("Invalid expiration time"); } return new Date(Date.now() + expTime); }) ); export { defaultSessionConfig, makeExpirationDate, makeSessionId };