在我的網站上,我有一個登錄表單,用戶在其中輸入他們的資訊,當他們提交發布請求時,會檢查他們的資訊,如果有效,我會將他們重定向回登錄表單,然后他們在其中輸入代碼發送到他們的電子郵件。我的問題是當第二次發出 post 請求時,我將如何使用相同的 url 但不必再次進行驗證。
Auth.js
//Login Route
router.post('/login', async(req, res) => {
//Validate Data
const { error } = loginValidation(req.body);
if (error) {
let msg = error.message;
return res.cookie('loginError', msg, [{ httpOnly: true }]).redirect('/login');;
}
//Check if user exists
const user = await User.findOne({ email: req.body.email });
if (!user) {
let msg = 'Email or password is invalid!'
return res.cookie('loginError', msg, [{ httpOnly: true }]).redirect('/login');;
}
//Check if password is correct
const validPass = await bcrypt.compare(req.body.password, user.password);
if (!validPass) {
let msg = 'Email or password is invalid!'
return res.cookie('loginError', msg, [{ httpOnly: true }]).redirect('/login');;
}
const verificationCode = Math.floor(100000 Math.random() * 900000);
email.loginCode(req.body.email, verificationCode);
return res.cookie('formComplete', 'true', [{ httpOnly: true }]).redirect('/login');
//Create and assign a jwt
const token = jwt.sign({ _id: user._id }, process.env.TOKEN_SECRET);
res.header('auth-token', token).redirect('/dashboard');
});
抱歉,如果這是一個愚蠢的問題,我是 express.js 的新手,如果您需要更多資訊,請告訴我。
uj5u.com熱心網友回復:
實際上,它與快遞無關。
您正在詢問服務器中的“狀態”。
如果你要“重構”你的問題,它可能會變成“我有這個用戶已經登錄,如何在他訪問任何路線之前驗證他”
解決方案可以是幾個
- 添加全域狀態。let say
isVerified= false,第一次驗證后,你設定為true - 正如您在上面的“重構”問題中看到的那樣,
session您可以使用它來檢查用戶登錄或購物車......
我建議您深入研究上述兩種解決方案,例如第一個解決方案,在哪里宣告該全域狀態?如果我們需要檢查多個用戶怎么辦?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/465853.html
標籤:javascript 节点.js 表示 邮政
