I have used expressJs and user auth for jsonwebtoken
Here is my code that I have tried:
import jwt from "jsonwebtoken"; import User from "../models/userModel.js"; import asyncHandler from "express-async-handler"; const protect = asyncHandler(async (req, res, next) => { let token; console.log(req.headers.authorization); if ( req.headers.authorization && req.headers.authorization.startsWith("Bearer") ) { try { token = req.headers.authorization.split(" ")[1]; const decoded = jwt.verify(toekn, process.env.JWT_SECRET); console.log(decoded); next(); } catch (error) {} } if (!token) { res.status(401); throw new Error("token is not found"); } next(); }); export { protect }; My problem is, when I run this code with postman then just run console.log(req.headers.authorization); this line then finished with next() never meet my if condition.
where is any mistake!
Any suggestion, please.
https://stackoverflow.com/questions/67249671/why-didnt-run-inside-condition April 25, 2021 at 12:06PM
没有评论:
发表评论