zinotrust / pinvent-app

165 stars 110 forks source link

Get User Profile Data Route | Token Null Issue #6

Open mangala-karunarathne opened 1 year ago

mangala-karunarathne commented 1 year ago

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...

mangala-karunarathne commented 1 year ago

@zinotrust @kennethvega Can you please explain this issue...

Bek85 commented 1 year ago

@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

Bek85 commented 1 year ago

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...

image

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.

IOS-Codex commented 1 year ago

// 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.

srikanth1272 commented 1 year ago

// 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

IOS-Codex commented 1 year ago

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.

srikanth1272 commented 1 year ago

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;

IOS-Codex commented 1 year ago

try logging in to console the token from req.cookies.token

Manan5911 commented 1 year ago

req.cookies.token is printing "undefined". Can anyone please help!!