Backend: code formatted

This commit is contained in:
2024-05-12 21:25:52 +02:00
parent fa25015472
commit bf791db47f
26 changed files with 574 additions and 469 deletions

View File

@@ -1,9 +1,9 @@
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import env from '../config/environment';
import { Log } from 'nork';
import User from '../models/User';
import { isValidObjectId } from 'mongoose';
import { Request, Response, NextFunction } from "express";
import jwt from "jsonwebtoken";
import env from "../config/environment";
import { Log } from "nork";
import User from "../models/User";
import { isValidObjectId } from "mongoose";
export function requireAuth(req: Request, res: Response, next: NextFunction) {
const token = req.cookies?.jwt;
@@ -12,40 +12,40 @@ export function requireAuth(req: Request, res: Response, next: NextFunction) {
jwt.verify(token, env.JWT_SECRET, async (err: any, decodedToken: any) => {
if (err) {
// console.error(err.message)
res.status(401).send(Log.error(401, 'user is not authenticated'));
res.status(401).send(Log.error(401, "user is not authenticated"));
}
if (!err) {
const user = await User.findById(decodedToken.id);
if (user === null) {
res.status(401).send(Log.error(401, 'user is not authenticated'));
res.status(401).send(Log.error(401, "user is not authenticated"));
return;
}
res.locals.user = user;
Log.info(100, 'user is authenticated');
Log.info(100, "user is authenticated");
next();
}
});
}
if (!token) {
res.status(401).send(Log.error(401, 'user is not authenticated'));
res.status(401).send(Log.error(401, "user is not authenticated"));
}
}
export function requireVerified(req: Request, res: Response, next: NextFunction) {
if (res.locals.user._id) {
if (res.locals.user.verified) {
Log.info(100, 'user is verified');
Log.info(100, "user is verified");
next();
return;
}
res.status(403).json(Log.error(403, 'user is not verified'));
res.status(403).json(Log.error(403, "user is not verified"));
return;
}
if (!res.locals.user._id) {
res.status(401).send(Log.error(401, 'user is not authenticated'));
res.status(401).send(Log.error(401, "user is not authenticated"));
return;
}
}
@@ -53,33 +53,33 @@ export function requireVerified(req: Request, res: Response, next: NextFunction)
export class requireRole {
static Admin(req: Request, res: Response, next: NextFunction) {
if (res.locals.user.admin) {
Log.info(100, 'user is admin');
Log.info(100, "user is admin");
next();
return;
}
res.status(403).json(Log.error(403, 'insufficient permissions'));
res.status(403).json(Log.error(403, "insufficient permissions"));
return;
}
static Owner(req: Request, res: Response, next: NextFunction) {
try {
if (!isValidObjectId(req.body.domain_id)) {
throw Log.error(400, 'neznámé domain_id');
throw Log.error(400, "neznámé domain_id");
}
const domain = res.locals.user.domains.filter((domain: any) => domain.domain_id == req.body.domain_id);
console.log(domain);
if (domain.length < 1) {
throw Log.error(400, 'neznámé domain_id');
throw Log.error(400, "neznámé domain_id");
}
if (domain[0].role == 1) {
Log.info(100, 'user is owner');
Log.info(100, "user is owner");
next();
return;
}
res.status(403).json(Log.error(403, 'insufficient permissions'));
res.status(403).json(Log.error(403, "insufficient permissions"));
return;
} catch (err: any) {
res.status(400).json(err);
@@ -87,4 +87,4 @@ export class requireRole {
}
static Editor(req: Request, res: Response, next: NextFunction) {}
static Guest(req: Request, res: Response, next: NextFunction) {}
}
}