Something went wrong. Try again.
A lightweight fork of Quenti, the popular open-source alternative to Quizlet, with a reduced scope in mind.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import type { Context } from "elysia";
import { SESSION_COOKIE_NAME, type SessionUser, getSessionUser } from "@quenti/auth/session";
export class HttpError extends Error { constructor( public status: number, message: string, ) { super(message); }}
export const getCurrentUser = async ( cookie: Context["cookie"],): Promise<SessionUser | null> => { const token = cookie[SESSION_COOKIE_NAME]?.value as string | undefined; return getSessionUser(token);};
/** session required, not banned */export const requireAuthed = (user: SessionUser | null): SessionUser => { if (!user) throw new HttpError(401, "Not authenticated"); if (user.banned) throw new HttpError(403, "Account banned"); return user;};
// Username is set at signup now (no separate onboarding step), so this is// just an alias kept so call sites elsewhere don't need to change.export const requireOnboarded = requireAuthed;
export const requireTeacher = (user: SessionUser | null): SessionUser => { const authed = requireAuthed(user); if (authed.type !== "Teacher") throw new HttpError(403, "Teacher only"); return authed;};
export const setSessionCookie = ( cookie: Context["cookie"], token: string, expires: Date,) => { cookie[SESSION_COOKIE_NAME]?.set({ value: token, httpOnly: true, sameSite: "lax", path: "/", expires, });};
export const clearSessionCookie = (cookie: Context["cookie"]) => { cookie[SESSION_COOKIE_NAME]?.remove();};