1 year ago
#295372
Klem Lloyd Mwenya
How can I fix (node:14000) UnhandledPromiseRejectionWarning: Error: Expected "payload" to be a plain object. ...at validate?
I am relatively new to Promises, jwt, and bcryptjs, and I am developing an API with a custom user authentication which is supposed to check if a user with a unique email exists, if not responds within appropriate data object to the client, otherwise, check with bcrytjs if the stored password hash matches the given password. Then serialize that user into a jwt token, and then send a data object to the client-side. My issue is that am getting a weird error, happening when my API hits the Serialize User section. Where and how am I going wrong?
(node:14000) UnhandledPromiseRejectionWarning: Error: Expected "payload" to be a plain object.
at validate (C:\Users\user\Desktop\money-crown\mc_backend\node_modules\jsonwebtoken\sign.js:40:11)
at validatePayload
Here below is my API code:
const bcrypt = require("bcryptjs");
const express = require("express");
const jwt = require("jsonwebtoken");
const authFunctions = require("../config/authFunctions");
const User = require("../models/User");
const router = express.Router();
// Login
router.post("/login", (req, res) => {
let { email, password } = req.body;
// Reformat Data
email = email.toLowerCase();
User.findOne({ email: email }).then((user) => {
if (!user)
return res.send({
msg: "Tha Email Does NOT Exist!",
success: false,
});
else {
bcrypt.compare(password, user.password).then((isMatch) => {
if (!isMatch)
return res.send({
msg: "Wrong Password!",
success: false,
});
else {
// Serialize User
const accessToken = authFunctions.generateAccessToken(user);
const refreshToken = jwt.sign(user, process.env.REFRESH_TOKEN_SECRET);
res.send({
accessToken,
msg: "You Have Successfully Logged In.",
refreshToken,
success: true,
});
}
});
}
});
});
node.js
mongodb
mongoose
jwt
bcryptjs
0 Answers
Your Answer