UNPKG

892 BPlain TextView Raw
1/**
2 * This module contains a set of standard verifiers to ensure user authentication
3 * and authorization.
4 */
5
6import * as express from "express";
7import Role from "./entities/role.entity";
8import Request from "./request";
9
10/**
11 * Verify that the user is authenticated.
12 */
13export function authUser(req: Request, _: express.Response) {
14 if (!req.user || req.user === undefined) {
15 return false;
16 }
17 return true;
18}
19
20/**
21 * Verify that the user is NOT authenticated.
22 */
23export function notAuthUser(req: Request, res: express.Response) {
24 return !authUser(req, res);
25}
26
27/**
28 * Verify that the user is logged and that is at least a Staff member.
29 */
30export function isStaffOrGreater(req: Request, res: express.Response) {
31 if (!authUser(req, res)) {
32 return false;
33 }
34 if (req.user.level >= Role.STAFF_LEVEL) {
35 return true;
36 }
37 return false;
38}