30 lines
906 B
TypeScript
30 lines
906 B
TypeScript
|
import { SessionContext } from "../api/jrpc";
|
||
|
|
||
|
export function requireLoginState(ctx: SessionContext, validated: boolean = true, special: boolean = false): boolean {
|
||
|
if (!ctx.user) return false;
|
||
|
if (validated && !ctx.session.validated) return false;
|
||
|
|
||
|
if (special) {
|
||
|
//TODO: Implement something...
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
export function RequireLogin(validated = true, special = false) {
|
||
|
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
||
|
let original = descriptor.value;
|
||
|
|
||
|
descriptor.value = function (...args: any[]) {
|
||
|
let ctx = args[args.length - 1] as SessionContext;
|
||
|
if (!ctx) throw new Error("Invalid request");
|
||
|
|
||
|
if (!requireLoginState(ctx, validated, special)) {
|
||
|
throw new Error("Not logged in");
|
||
|
}
|
||
|
|
||
|
return original.apply(this, args);
|
||
|
}
|
||
|
}
|
||
|
}
|