Search code examples
mysqlnode.jsunhandled-promise-rejection

How to resolve UnhandledPromiseRejectionWarning?


Hello and good day to you.

I have a problem. I can register a user, but when I log-in that user, it loads forever and displays an: "UnhandledPromiseRejectionWarning: ReferenceError: Invalid left-hand side in assignment"

So this is the content of my users table. enter image description here

Structure of the Users Table enter image description here

The UnhandledPromiseRejection error enter image description here

Basically, it says that I have a problem on my controllers/auth.js, on line 30. enter image description here

This is my full code on auth.js.

exports.signin = (req, res) => {
    try {
        const {email, password} = req.body;
        if(!email || !password) {
            return res.send("<script> alert('Provide an email and/or Password'); window.location='/signin'; </script>");
        }
        con.query('SELECT * FROM users WHERE email = ?', [email], async (error, results) => {
            console.log(results);
            if(!results || !(await bcrypt.compare(password, results[0].password))) {
                return res.send("<script> alert('Email or Password is incorrect'); window.location='/signin'; </script>");
            }
            else {
                const id = results[0].id;
                const token = jwt.sign({ id }, process.env.JWT_SECRET, {
                    expiresIn: process.env.JWT_EXPIRES_IN
                });

                console.log("The token is: " + token);
                const cookieOptions = {
                    expires: new Date(
                        Date.now() = process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000
                    ),
                    httpOnly: true
                }
                res.cookie('jwt', token, cookieOptions);
                res.status(200).redirect("/profile");
            }
        });
    }
    catch(error) {
        console.log(error);
    }
}

exports.signup = (req, res) => {
    console.log(req.body);

    const {name, email, password, passwordConfirm} = req.body;
    con.query('SELECT email FROM users WHERE email = ?', [email], async (error, results) => {
        if(error) {
            console.log(error);
        }
        if(results.length > 0) {
            return res.send("<script> alert('This email is already in use or invalid.'); window.location='/signup'; </script>");
        }
        else if(password !== passwordConfirm) {
            return res.send("<script> alert('Passwords do not match.'); window.location='/signup'; </script>");         
        }

        let hashedPassword = await bcrypt.hash(password, 8);
        console.log(hashedPassword);

        con.query('INSERT INTO users SET ?', {name: name, email: email, password: hashedPassword}, (error, results) => {
            if(error) {
                console.log(error);
            }
            else {
                console.log(results);
                return res.send("<script> alert('USER IS REGISTERED! You are redirected to Sign-in Page.'); window.location='/signin'; </script>");      
            }
        });
    });
}

I watched his channel and copied the code. https://www.youtube.com/watch?v=VavWEtI5T7c&ab_channel=TelmoSampaio


Solution

  • The problem is the following line:

    expires: new Date(Date.now() = process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000),
    

    You are trying to assign a value, but it probably should be an addition of the expiration to the current date:

    expires: new Date(Date.now() + process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000),