Files
website/server/utils/enforceAuth.ts
2026-08-12 21:07:49 +02:00

30 lines
655 B
TypeScript

import type { H3Event } from 'h3';
export type AuthContext = {
pid: number;
username: string;
token: string;
email: string;
accessLevel: number;
stripeSubscriptionId: string | null;
};
export function setAuthContext(event: H3Event, context: AuthContext | null): void {
event.context.auth = context;
}
export function getAuthContext(event: H3Event): AuthContext | null {
return event.context.auth ?? null;
}
export function enforceLoggedIn(event: H3Event): AuthContext {
const context = getAuthContext(event);
if (!context) {
throw createError({
status: 401,
message: 'This action requires authentication'
});
}
return context;
}