|
|
|
|
@ -1,15 +1,15 @@
|
|
|
|
|
import { Response, NextFunction } from "express";
|
|
|
|
|
import jsonwebtoken, { JsonWebTokenError } from "jsonwebtoken";
|
|
|
|
|
import { AuthedRequest } from "../interfaces/auth";
|
|
|
|
|
import { AuthedRequest, UserTokenData } from "../interfaces/auth";
|
|
|
|
|
import { public_key } from "../environment"
|
|
|
|
|
|
|
|
|
|
export const UserAuth = (req: AuthedRequest, res: Response, next: NextFunction) => {
|
|
|
|
|
if (req.cookies["auth-token"] === undefined) return res.sendStatus(401);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const data: any = jsonwebtoken.verify(req.cookies["auth-token"], public_key);
|
|
|
|
|
const data: UserTokenData | any = jsonwebtoken.verify(req.cookies["auth-token"], public_key);
|
|
|
|
|
|
|
|
|
|
if (data.user_type !== "User") {
|
|
|
|
|
if (data.tokenType !== "User") {
|
|
|
|
|
return res.sendStatus(403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -25,9 +25,13 @@ export const TrainerAuth = (req: AuthedRequest, res: Response, next: NextFunctio
|
|
|
|
|
if (req.cookies["auth-token"] === undefined) return res.sendStatus(401);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const data: any = jsonwebtoken.verify(req.cookies["auth-token"], public_key);
|
|
|
|
|
const data: UserTokenData | any = jsonwebtoken.verify(req.cookies["auth-token"], public_key);
|
|
|
|
|
|
|
|
|
|
if (data.user_type !== "Trainer") {
|
|
|
|
|
if (data.tokenType !== "User") {
|
|
|
|
|
return res.sendStatus(403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.isTrainer !== true) {
|
|
|
|
|
return res.sendStatus(403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -43,9 +47,13 @@ export const AdminAuth = (req: AuthedRequest, res: Response, next: NextFunction)
|
|
|
|
|
if (req.cookies["auth-token"] === undefined) return res.sendStatus(401);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const data: any = jsonwebtoken.verify(req.cookies["auth-token"], public_key);
|
|
|
|
|
const data: UserTokenData | any = jsonwebtoken.verify(req.cookies["auth-token"], public_key);
|
|
|
|
|
|
|
|
|
|
if (data.tokenType !== "User") {
|
|
|
|
|
return res.sendStatus(403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.user_type !== "Admin") {
|
|
|
|
|
if (data.isAdmin !== true) {
|
|
|
|
|
return res.sendStatus(403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|