finished user store in typscript

This commit is contained in:
canove
2020-09-14 18:54:36 -03:00
parent eba3553a2d
commit 7f33e33078
29 changed files with 260 additions and 133 deletions

View File

@@ -0,0 +1,35 @@
import { verify } from "jsonwebtoken";
import { Request, Response, NextFunction } from "express";
import AppError from "../errors/AppError";
import authConfig from "../config/auth";
interface TokenPayload {
id: string;
username: string;
profile: string;
iat: number;
exp: number;
}
const isAuth = (req: Request, res: Response, next: NextFunction): void => {
const authHeader = req.headers.authorization;
if (!authHeader) {
throw new AppError("Token not provided.", 403);
}
const [, token] = authHeader.split(" ");
const decoded = verify(token, authConfig.secret);
const { id, profile } = decoded as TokenPayload;
req.user = {
id,
profile
};
return next();
};
export default isAuth;