I am writing a login function in myUserController which looks like this: async login(userToLogin) { // identifier can be username or email const validatedInput = loginInputValidation(userToLogin); const { error } = validatedInput.validatedInput; if (error) return error.details[0].message; const user = findUser(userToLogin); if (!user) return { success: false, message: 'We dont know this user! Do you want to register?' }; // return user; const token = issueToken(user, userToLogin); return token; } } The problem is whit the function issueToken which looks like thisconst issueToken = async (user, userToLogin) => { // return user.password; // return user; try { const validPass = await bcrypt.compare(userToLogin.password, user.password); if (!validPass) return { success: false, message: 'Invalid Password' }; } catch (error) { return error } const token = await jwt.sign({ _id: user._id }, process.env.TOKEN_SECRET); return { success: true, token: token }; }; The odd thing is that my user object is cleared when I try to access a property. I can only access the user properties in the function where I searched for that user, but when I return the whole user back to the frontend I can see the user object. Why is that?
Submitted September 16, 2020 at 06:15PM by snake_py
No comments:
Post a Comment