mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-08-28 19:46:35 -05:00
30 lines
655 B
TypeScript
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;
|
|
}
|