Files
auth-gatekeeper/src/middleware/initialize.ts
2023-05-30 20:22:12 -04:00

31 lines
748 B
TypeScript

import { Context, Middleware } from 'koa';
import { GatekeeperUser } from '../types/GatekeeperUser';
declare module 'koa' {
interface ExtendableContext {
user: GatekeeperUser | null;
isAuthenticated(): boolean;
isUnauthenticated(): boolean;
logout(): void;
}
}
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 = isAuthenticated.bind(ctx);
ctx.isUnauthenticated = isUnauthenticated.bind(ctx);
ctx.logout = logout.bind(ctx);
};