Gatekeeper! Better
This commit is contained in:
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user