Feat. Api send

This commit is contained in:
Wender Teixeira
2021-12-30 19:18:30 -03:00
parent d6efb23136
commit bce9566b6e
9 changed files with 221 additions and 5 deletions

View File

@@ -0,0 +1,39 @@
import { Request, Response, NextFunction } from "express";
import AppError from "../errors/AppError";
import ListSettingByValueService from "../services/SettingServices/ListSettingByValueService";
const isAuthApi = async (
req: Request,
res: Response,
next: NextFunction
): Promise<void> => {
const authHeader = req.headers.authorization;
if (!authHeader) {
throw new AppError("ERR_SESSION_EXPIRED", 401);
}
const [, token] = authHeader.split(" ");
try {
const getToken = await ListSettingByValueService(token);
if (!getToken) {
throw new AppError("ERR_SESSION_EXPIRED", 401);
}
if (getToken.value !== token) {
throw new AppError("ERR_SESSION_EXPIRED", 401);
}
} catch (err) {
console.log(err);
throw new AppError(
"Invalid token. We'll try to assign a new one on next request",
403
);
}
return next();
};
export default isAuthApi;