有没有办法允许用户使用他的密码,电子邮件和姓名注册本地策略?
我在网上找到的每个例子都只使用名称/密码或电子邮件/密码.
我还搜索了整个护照文档,但该文档根本没用.这只是一个充满例子的臃肿网站.
我只需要一份护照使用的函数,类和变量列表,并解释它们和它们的每个参数的作用.每个好的图书馆都有这样的东西,为什么我找不到护照?
以下是我的代码的关键部分:
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" }));
护照真的那么有限吗?必须有办法做到这一点,对吧?
您可能会有点困惑,但护照不会实施注册方法.它只是授权库.所以你必须自己处理这个用例.
首先,创建负责注册和检查的路线:
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框架,但您可以适应它而不会出现问题.
下一步是包括护照本地策略.
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));
我们现在只有登录任务.这很简单.
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); }
这种方式更适合护照,对我来说很有用.
这是对我有用的解决方案,该解决方案基于基于猫鼬的odm,第一部分是护照相关部分,我还将odm的用户部分附加到谁进行密码加密的工作上。
如果我理解您的问题,则希望用户输入他的电子邮件或密码。在这种情况下,修改搜索以尝试两者,即,将提供的用户标识符(在您对findOne(...)的调用中)与用户名或密码匹配。
请注意,我使用bcrypt来避免存储明确的密码,这就是为什么有一种用于测试密码的自定义比较方法的原因。还要注意使用Google Auth的“提示”,“我的系统”同时启用了这两个功能,如果相关,请记住,我可以添加所需的代码。
------------身份验证部分(仅相关片段)-----------
var passport = require('passport'), LocalStrategy = require('passport-local').Strategy; passport.serializeUser(function(user, done) { // the values returned here will be used to deserializeUser // this can be use for further logins done(null, {username: user.username, _id: user.id, role: user.role}); }); passport.deserializeUser(function(user, done) { done(null, user); }); passport.use(new LocalStrategy(function(username, password, done){ odm.User.findOne({username: username, authType: 'direct'}, function(err, user){ if(err){ return done(err, false); } if(!user){ return done(null, false); } if(user.role === 'new'){ console.log('can not use new user!'); return done('user not activated yet, please contact admin', false); } user.comparePassword(password,function(err, isMatch){ if(err){ return done(err, false); } if(isMatch){ return done(null, user);//{username: username}); } return done(null, false); }); }); })); app.post('/login', function(req, res, next){ passport.authenticate('local', { failureRedirect: '/logout?status=login failed' }, function(err, user, info){ if(err){ return next(err); } if(!user){ return res.redirect('/login'); } req.logIn(user, function(err){ if (req.body.rememberme) { req.session.cookie.maxAge = 30*24*60*60*1000 ;//Rememeber 'me' for 30 days } else { req.session.cookie.expires = false; } var redirect = req.param('redirect') || '/index'; res.redirect(redirect); }); } )(req, res, next); } ); app.post('/register',function(req, res){ var user = new odm.User({username: req.body.username, password: req.body.password, email: req.body.email, authType: 'direct'}); user.save(function(err, user){ if(err){ console.log('registration err: ' , err); } else { res.redirect('/list'); } }); });
---用户/ odm,相关部分----------------
var bcrypt = require('bcrypt-nodejs'); // --------------------- User ------------------------------------------ // var userSchema = new Schema({ name: String, email: String, username: {type: String, required: true, unique: true}, password: String, role: {type: String, required: true, enum: ['new', 'admin', 'user'], default: 'new'}, authType: {type: String, enum: ['google', 'direct'], required: true} }); userSchema.pre('save', function (next) { var user = this; if (!user.isModified('password')) return next(); console.log('making hash...........'); bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) { if (err) return next(err); bcrypt.hash(user.password, salt, null, function (err, hash) { if (err) return next(err); user.password = hash; next(); }); }); }); userSchema.methods.comparePassword = function (candidatePassword, cb) { bcrypt.compare(candidatePassword, this.password, function (err, isMatch) { if (err) return cb(err); cb(null, isMatch); }); };