I'm starting the app and getting this error:
email: req.user.emails[0].value, ^ TypeError: Cannot read properties of undefined (reading '0')
It was working good until I added the same code but for facebook login, it was reading and storing but I don't why it stopped.
The code:
router.get("/googleLogin/success", async (req, res)=>{
if(req.user){
const user = await User.findOne({provider_id: req.user.id,
provider: req.user.provider})
if(user){
res.status(200).json({
success: true,
message: "success",
user: user
})
console.log("GOOGLE USER IS: " + user)
}else{
const checkUserEmail = await User.findOne({email: req.user.email})
if(checkUserEmail){
res.status(401).json({
success: false,
message: "User already Exist with this email id",
})
}else{
const user = await User.create({
username: req.user.name.givenName+ "_" +req.user.name.familyName,
firstName: req.user.name.givenName,
lastName: req.user.name.familyName,
// THE ERROR IS HERE
email: req.user.emails[0].value,
provider: req.user.provider,
provider_id: req.user.id,
profilePic: req.user.photos[0].value,
});
res.status(200).json({
success: true,
message: "success",
user: user
})
}
}
console.log("CURRNT USER: ", user);
}
})
Basically the
req.user.emailsisundefined.You are likely missing the
emailscope inprofileFieldsoption.and
See Passport-Facebook authentication is not providing email for all Facebook accounts