win*_*toy 5 node.js express passport.js
我想通过生成随机用户名和密码自动生成用户帐户,然后用户自动登录(用户不知道他的用户名/密码,他的浏览器只存储会话cookie).
Passport作为中间件,所以如何验证我刚刚生成的用户?或者,以某种方式重定向到我的app.post('/login')路线并发送这些变量会更好吗?(但不知何故将这些发送到浏览器,只是被发送回服务器似乎不是非常安全或有效).
app.get('/signup', function(req, res) {
if(req.isAuthenticated()) { res.redirect('/'); }
else {
var today = new Date();
var weekDate = new Date();
weekDate.setDate(today.getDate() + 7);
var key1 = Math.random().toString();
var key2 = Math.random().toString();
var hash1 = crypto.createHmac('sha1', key1).update(today.valueOf().toString()).digest('hex');
var hash2 = crypto.createHmac('sha1', key2).update(weekDate.valueOf().toString()).digest('hex');
var newUser = new models.User({
username: hash1,
password: hash2,
signupDate: today,
accountStatus: 0,
expirationDate: weekDate,
});
newUser.save(function(err) {
if(err) {}
console.log("New user created.");
//HOW CAN I PASS USERNAME AND PASSWORD ARGUMENTS???
passport.authenticate('local')();
res.redirect('/login');
})
}
});
Run Code Online (Sandbox Code Playgroud)
更换你的电话到passport.authenticate('local')();与
req.logIn(user, function(err) {
if (err) { return next(err); }
//copied from the docs, you might want to send the user somewhere else ;)
return res.redirect('/users/' + user.username);
});
Run Code Online (Sandbox Code Playgroud)
让我知道这是怎么回事.
| 归档时间: |
|
| 查看次数: |
2544 次 |
| 最近记录: |