使用Passport进行密码确认

时间:2017-03-25 14:10:54

标签: node.js passwords ejs passport.js

我已经创建了一个基本的用户注册表单,该表单工作正常,但我需要添加一个确认密码字段。

我的表格如下:

<div class ="container form">

<div class="jumbotron form"><h2><i class="fa fa-user-plus" aria-hidden="true"></i> Signup</h2></div>
<form action = "/register" method="POST">
<div class="form-group">
    <i class="fa fa-user" aria-hidden="true"></i>
    <label for="username">Username</label>
        <input type = "text" class = "form-control" placeholder = "Enter username" name="username"> 
</div>
<div class="form-group">
    <i class="fa fa-key" aria-hidden="true"></i>
    <label for="password">Password</label>
        <input type = "password" class ="form-control" placeholder = "Enter password" name="password1">
</div>
<div class="form-group">
    <i class="fa fa-key" aria-hidden="true"></i>
    <label for="password">Confirm password</label>
        <input type = "password" class ="form-control" placeholder = "Enter password" name = "password">
</div>
<div class="form-group">
<i class="fa fa-picture-o" aria-hidden="true"></i>
    <label for="img">Image</label>
        <input type = "text" class ="form-control" placeholder = "Enter image URL" name = "image">
</div>
    <button type ="submit" class="btn btn-primary btn-lg">Signup</button>
</form>
</div>

因此,当用户点击提交时,我显然需要2个密码输入才能匹配,如果他们不这样做就会抛出错误。

我使用Passport进行身份验证。我有以下设置:

// passport configuration
app.use(require("express-session")({
secret: "maidenJones",
resave: false,
saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(expressSanitizer());

app.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});

注册路线:

router.post("/register", function(req, res){
var newUser = new User({username: req.body.username, image: req.body.image});
User.register(newUser, req.body.password, function(err, user){
if(err){
    res.redirect("/blogs");
    console.log(err);
} 
passport.authenticate("local")(req, res, function(){
    res.redirect("/blogs");
});
});
});

不确定从何处开始实施密码确认检查

2 个答案:

答案 0 :(得分:0)

您必须将注册逻辑移到护照LocalStrategy中,然后将该策略用作路由中的中间件。

示例:

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'passwd'
},
function(req, username, password, done) {
    // User register logic
  }
));

router.post('/register', 
    passport.authenticate('local', { failureRedirect: '/register' }),
    function(req, res) {
    res.redirect('/blogs');
});

此处有更多信息:passport-localstrategy

答案 1 :(得分:0)

passport.use('local.signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true

},

function (req, email, password, done) {
    //All validation logic comes here
    let password1 = req.body.password;
    let password2 = req.body.password2;
    req.checkBody('email', 'Invalid email').notEmpty().isEmail();
    req.checkBody('password', 'Password must be more than 4 characters')
        .not().isEmpty().isLength({ min: 4 });
    let errors = req.validationErrors();
    //just to see the output in your console
    console.log(password1);
    console.log(password2);
    if (errors) {
        let messages = [];
        errors.forEach(function (error) {
            messages.push(error.msg);
        });
        return done(null, false, req.flash('error', messages));

    } else if (password1 !== password2) {
        return done(null, false, req.flash('error', 'Password and Confirm Password must match'));
    }
     // You can then continue your code logic(submitting to database)