2

你好,祝你有美好的一天。

我有个问题。我可以注册一个用户,但是当我登录该用户时,它会永远加载并显示:“UnhandledPromiseRejectionWarning: ReferenceError: Invalid left-hand side in assignment”

所以这是我的用户表的内容。 在此处输入图像描述

用户表的结构 在此处输入图像描述

UnhandledPromiseRejection 错误 在此处输入图像描述

基本上,它说我的控制器/auth.js 有问题,在第 30 行。 在此处输入图像描述

这是我在 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>");      
            }
        });
    });
}

我看了他的频道并复制了代码。 https://www.youtube.com/watch?v=VavWEtI5T7c&ab_channel=TelmoSampaio

4

1 回答 1

3

问题是以下行:

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

您正在尝试分配一个值,但它可能应该是将到期时间添加到当前日期:

expires: new Date(Date.now() + process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000),
于 2020-12-09T09:39:44.763 回答