通过护照Js注册用户

我想通过passport.js的帮助从注册页面添加新用户registry单如下

<form id="Signup-form" name="SignupForm" action="/signup" method="post"/> <input type="text" id="firstname" name="Firstname" > <input type="text" id="lastname" name="Lastname"/> <input type="email" name="email" /> <input type="text" id="rollno" name="rollno"/> <input type="password" name="password" id="password"/> <input type="password" name="confirm" id="confirm-password"/> <input type="radio" name='Gender' value="Male" /> <input type="radio" name='Gender' value="FeMale" /> </form> 

我的护照在app.js中初始化为

需要

 var passport = require('passport'); var LocalStrategy = require('passport-local').Strategy; 

数据库设置后

 require('./config/passport'); 

初始化为

 app.use(passport.initialize()); app.use(passport.session()); 

张贴路线

 router.post('/signup', passport.authenticate('local.signup' , { successRedirect : '/home', failuerRedirect : '/signup', failuerFlash: true })); 

我的用户模型

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var bcrypt = require('bcrypt-nodejs') const UserSchema = new Schema({ First_Name : String, Last_Name : String, email : String, Roll_No : String, Gender : String, password : String },{collection : 'Users'}); UserSchema.methods.encryptPassword = function(password) { return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null); }; UserSchema.methods.validPassword = function(password) { return bcrypt.compareSync(password, this.password); } var User = mongoose.model('User' , UserSchema); module.exports = User; 

现在我的configport目录中的passport.js文件是

 var passport = require('passport'); var User = require('../models/user'); var LocalStrategy = require('passport-local').Strategy; passport.serializeUser(function (user, done) { done(null, user.id); }); passport.deserializeUser(function (id, done) { User.findById(id, function (err, user) { done(err, user); }); }); 

我的主要问题是如何为所有领域的这条路线写策略

 passport.use('local.signup', new LocalStrategy({ //strategy code here })); 

这里是一个很好的例子, 简单节点身份validation:安装和本地

 passport.use('local-signup', new LocalStrategy({ // by default, local strategy uses username and password, we will override with email usernameField : 'email', passwordField : 'password', passReqToCallback : true // allows us to pass back the entire request to the callback }, function(req, email, password, done) { // find a user whose email is the same as the forms email // we are checking to see if the user trying to login already exists User.findOne({ 'local.email' : email }, function(err, user) { // if there are any errors, return the error if (err) return done(err); // check to see if theres already a user with that email if (user) { return done(null, false, req.flash('signupMessage', 'That email is already taken.')); } else { // if there is no user with that email // create the user var newUser = new User(); // set the user's local credentials newUser.local.email = email; newUser.local.password = newUser.generateHash(password); // save the user newUser.save(function(err) { if (err) throw err; return done(null, newUser); }); } })); 

使用下面的链接进行login注册使用护照https://github.com/sourabhkum/expressapp