Open mangala-karunarathne opened 1 year ago
@zinotrust @kennethvega Can you please explain this issue...
@zinotrust @kennethvega Can you please explain this issue...
const protect = asyncHandler(async (req, res, next) => {
try { const token = req.token; if (!token) { res.status(401); throw new Error(" Not Authorized, Please login..."); }
// Verify Token const verified = jwt.verify(token, process.env.JWT_SECRET); // Get User id from token const user = await User.findById(verified.id).select("-password"); if (!user) { res.status(401); throw new Error(" User doesn't exists"); } user = res.user ; next();
} catch (error) { res.status(401); throw new Error("Not Authorized, Please login..."); } });
Here we are experiencing issue when getting user profile data in token. We just put console.log for token and got as Null. Can you plz explain the reason behind there...
if you are getting "null" in your backend, chances are, the "token" is not being sent in request.headers from frontend. Just double check your request in the frontend to make sure that the "token" is being sent in request headers
const protect = asyncHandler(async (req, res, next) => {
try { const token = req.token; if (!token) { res.status(401); throw new Error(" Not Authorized, Please login..."); }
// Verify Token const verified = jwt.verify(token, process.env.JWT_SECRET); // Get User id from token const user = await User.findById(verified.id).select("-password"); if (!user) { res.status(401); throw new Error(" User doesn't exists"); } user = res.user ; next();
} catch (error) { res.status(401); throw new Error("Not Authorized, Please login..."); } });
Here we are experiencing issue when getting user profile data in token. We just put console.log for token and got as Null. Can you plz explain the reason behind there...
after login, to fetch list of products for logged in user, the token is sent in "Request Headers" in the frontend. It should be exactly the same thing to request profile data.
// Get User id from token const user = await User.findById(verified._id).select("-password");
the id was passed as _id.
This will fix your issue.
// Get User id from token const user = await User.findById(verified._id).select("-password");
the id was passed as _id.
This will fix your issue.
here i am getting the error as token is undefined.can you please help me
Did you send the token and you saw it in the Browser inspector?
If that is the case, send the whole controller function so I can help debug.
help //I didn't completed the full app. I have Just started the backend upto getuser
//Usercontroller const getUser = asyncHandler(async (req, res) => { const user = await User.findById(req.user._id);
if (user) { const { _id, name, email } = user; res.status(200).json({ _id, name, email, }); } else { res.status(400); throw new Error("User Not Found"); } });
//Authmiddleware const asyncHandler = require("express-async-handler"); const User = require("../models/usermodel"); const jwt = require("jsonwebtoken");
const protect = asyncHandler(async (req, res, next) => { try { const token = req.cookies.token; if (!token) { res.status(401); throw new Error("Not authorized, please login"); }
// Verify Token
const verified = jwt.verify(token, process.env.JWT_SECRET);
// Get user id from token
const user = await User.findById(verified._id).select("-password");
if (!user) {
res.status(401);
throw new Error("User not found");
}
req.user = user;
next();
} catch (error) { res.status(401); throw new Error("Not authorized, please login"); } });
module.exports = protect;
try logging in to console the token from req.cookies.token
req.cookies.token is printing "undefined". Can anyone please help!!
const protect = asyncHandler(async (req, res, next) => {
try { const token = req.token; if (!token) { res.status(401); throw new Error(" Not Authorized, Please login..."); }
} catch (error) { res.status(401); throw new Error("Not Authorized, Please login..."); } });
Here we are experiencing issue when getting user profile data in token. We just put console.log for token and got as Null. Can you plz explain the reason behind there...