Add JRPC API, reworked Login and User pages
This commit is contained in:
29
Backend/src/helper/login.ts
Normal file
29
Backend/src/helper/login.ts
Normal file
@ -0,0 +1,29 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user