Permission System (#47)

* feat(api): permissions system

Adds a permission system for isAuthenticated middleware. Also adds user CRUD.
This commit is contained in:
sct
2020-09-03 19:20:14 +09:00
committed by GitHub
parent 5d46f8d76d
commit cfc84ce2f3
8 changed files with 240 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
import { getRepository } from 'typeorm';
import { User } from '../entity/User';
import { Permission } from '../lib/permissions';
export const checkUser: Middleware = async (req, _res, next) => {
if (req.session?.userId) {
@@ -16,13 +17,18 @@ export const checkUser: Middleware = async (req, _res, next) => {
next();
};
export const isAuthenticated: Middleware = async (req, res, next) => {
if (!req.user) {
res.status(403).json({
status: 403,
error: 'You do not have permisson to access this endpoint',
});
} else {
next();
}
export const isAuthenticated = (
permissions?: Permission | Permission[]
): Middleware => {
const authMiddleware: Middleware = (req, res, next) => {
if (!req.user || !req.user.hasPermission(permissions ?? 0)) {
res.status(403).json({
status: 403,
error: 'You do not have permisson to access this endpoint',
});
} else {
next();
}
};
return authMiddleware;
};