Files
recipe-manager/server/auth/middlewares.ts
2023-02-11 16:25:30 -06:00

42 lines
1.0 KiB
TypeScript

import { NextFunction, Request, Response } from "express"
import dotenv from "dotenv";
import { IUser } from "../schemas";
dotenv.config();
export function restrictAccess(req: Request, res: Response, next: NextFunction) {
if (req.session.user == undefined) {
res.send("content restricted");
} else {
next();
}
}
export function requireSessionSecret(req: Request, res: Response, next: NextFunction) {
const secret = process.env.SESSIONSECRET;
if (!secret) {
res.sendStatus(500);
throw new Error("Express secret is undefined");
} else {
next();
}
}
export function checkSubscription(req: Request, res: Response, next: NextFunction) {
}
export function checkFriendStatus(req: Request, res: Response, next: NextFunction) {
}
export function checkIsAdmin(req: Request, res: Response, next: NextFunction) {
const user: IUser | undefined = req.user as IUser;
if (user.isadmin) {
next();
} else {
res.status(403).send("Unauthorized");
}
}