mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 04:09:26 +00:00
finished user store in typscript
This commit is contained in:
35
backend/src/middleware/isAuth.ts
Normal file
35
backend/src/middleware/isAuth.ts
Normal 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;
|
||||
Reference in New Issue
Block a user