问题描述
有没有办法允许用户使用他的密码,电子邮件和姓名注册本地策略?
我在网上找到的每个例子都只使用姓名/密码或电子邮件/密码。
Is there any way to allow a user to register on the local strategy with his password, email and name?
Every example I could find online only use name/password or email/password.
我还搜索了整个护照文档,但该文档根本没用。它只是一个充满示例的膨胀网站。
我只需要一份函数,类和变量护照使用的列表,并解释它们和它们的每个参数的作用。每个好的图书馆都有类似的东西,为什么我找不到护照呢?
I also searched through the the whole passport documentation, but that documentation isn't helpful at all. It's just one bloated site full of examples.
I just need an list of functions, classes and variables passport uses with explanations what they and every parameter of them do. Every good library has something like that, why can't I find it for passport?
以下是我的代码的关键部分:
Here are the key parts of my code:
passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
//are there other options?
//emailField did not seem to do anything
passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function(req, email, password, done) {
//check if email not already in database
//create new user using "email" and "password"
//I want an additional parameter here "name"
}));
护照真的有限吗?必须要有办法做到这一点,对吗?
So is passport really that limited? There has to be a way to do this, right?
推荐答案
你可能有点困惑,但护照没有实施注册方法。它只是授权库。因此,您必须自己处理该用例。
You can be a little confused but passport doesn't implement signup methods. It's just authorisation library. So you must handle that use-case on your own.
首先,创建负责注册和检查的路线:
First of all, create route that will be responsible for sign-up and your checks:
signup: function (req, res) {
User
.findOne({
or: [{username: req.param('username')}, {email: req.param('email')}]
})
.then(function(user) {
if (user) return {message: 'User already exists'};
return User.create(req.allParams());
})
.then(res.ok)
.catch(res.negotiate);
}
上面的示例基于Sails框架,但您可以使用它你个人的问题。
The example above is based on Sails framework, but you can fit it with no problems to your own case.
下一步是包括护照本地策略。
Next step is include passport local strategy.
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var LOCAL_STRATEGY_CONFIG = {
usernameField: 'email',
passwordField: 'password',
session: false,
passReqToCallback: true
};
function _onLocalStrategyAuth(req, email, password, next) {
User
.findOne(or: [{email: email}, {username: email}])
.then(function (user) {
if (!user) return next(null, null, {
code: 'E_USER_NOT_FOUND',
message: email + ' is not found',
status: 401
});
if (!HashService.bcrypt.compareSync(password, user.password)) return next(null, null, {
code: 'E_WRONG_PASSWORD',
message: 'Password is wrong',
status: 401
});
return next(null, user, {});
})
.catch(next);
}
passport.use(new LocalStrategy(LOCAL_STRATEGY_CONFIG), _onLocalStrategyAuth));
我们现在只有登录任务。这很简单。
We have only signin task now. It's simple.
signin: function(req, res) {
passport.authenticate('local', function(error, user, info) {
if (error || !user) return res.negotiate(Object.assign(error, info));
return res.ok(user);
})(req, res);
}
这种方式更适合护照,非常适合我。
This way is more suitable for passport and works great for me.
这篇关于护照:允许注册姓名和电子邮件地址? (地方战略)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!