问题描述
我正在为我的应用程序使用Express JS和Passport JS.
I'm using Express JS and Passport JS for my app.
我想为新用户提供一次通过特定URL自动登录一次的机会.我可以使用URL中的信息从数据库中获取用户,因此我有一个User对象(带有ID,电子邮件,哈希密码等),但是我不知道如何使用护照对用户进行身份验证和登录.
I want to give a new user the opportunity to automatically login, once, by a specific URL. I can get the user from the database with the information from the URL, so I have an User object (with id, email, hashed password etc.) but I don't know how I can use passport to authenticate the user and login.
我尝试使用从数据库中获取的用户对象执行以下功能:
I tried executing below function with the user object I got from the database:
req.login(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + req.user.username);
});
来源: http://passportjs.org/guide/login/
但这没用.猜猜这仅仅是因为用户对象包含哈希密码...以前曾经尝试过并可以告诉我它如何工作的人吗?
But that didn't work. Guess it's just because the user object contains the hashed password...Anyone who ever tried this before and can tell me how it works?
推荐答案
也许 https://github.com /yarax/passport-url 策略将对您有用
基本逻辑从url获取参数
Base logic is getting argument from url
UrlStrategy.prototype.authenticate = function(req, options) {
var self = this;
function verified(err, user, info) {
if (err) { return self.redirect(self.failRedirect); } // redirect in fail
self.success(user, info); // done callback
}
this._verify(req.query[this.varName], verified);
};
此处的完整示例 https://github.com/yarax/passport -url/blob/master/index.js
这篇关于Passport JS通过URL进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!