Gatekeeper! Better

This commit is contained in:
2023-05-30 20:22:12 -04:00
parent 1f3eb0b9c9
commit a0e30f95c3
12 changed files with 83 additions and 69 deletions

View File

@@ -1,5 +1,5 @@
import { Middleware } from 'koa';
import { GatekeeperUser } from './types/GatekeeperUser';
import { Context, Middleware } from 'koa';
import { GatekeeperUser } from '../types/GatekeeperUser';
declare module 'koa' {
interface ExtendableContext {
@@ -10,9 +10,21 @@ declare module 'koa' {
}
}
const isAuthenticated = function (this: Context) {
return !!this.user;
};
const isUnauthenticated = function (this: Context) {
return !this.user;
};
const logout = function (this: Context) {
this.user = null;
};
export const initialize: Middleware = (ctx) => {
ctx.user = null;
ctx.isAuthenticated = () => !!ctx.user;
ctx.isUnauthenticated = () => !ctx.user;
ctx.logout = () => (ctx.user = null);
ctx.isAuthenticated = isAuthenticated.bind(ctx);
ctx.isUnauthenticated = isUnauthenticated.bind(ctx);
ctx.logout = logout.bind(ctx);
};