File size: 650 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const jwt = require("jsonwebtoken");
require("dotenv").config();
const { SECRET_KEY_FOR_BCRYPT } = process.env;

module.exports = async (req, res, next) => {
	try {
		const jwtTokenFromClient = req.header("token");

		if (!jwtTokenFromClient) {
			return res.status(403).json("Not Authozied");
		}

		//jwt.verify compair 'token from client' and secretKeyFrom and if 'jwtTokenFromClient' and 'SECRET_KEY_FOR_BCRYPT' matched the its return payload
		const payload = jwt.verify(jwtTokenFromClient, SECRET_KEY_FOR_BCRYPT);

		req.user = payload;
		next();
	} catch (e) {
		console.error(e.message);
		return res.status(403).json("Not Authozied");
	}
};