我正在使用PassportJS和我的ExpressJS应用程序,我正在尝试设置密码确认字段,用户注册。我最初的想法是使这个视图/控制器设置而不是涉及模型来简化这个过程,但是我遇到的问题是我试图实现这个逻辑,因为我的passportJS localStrategy没有检查两个字段值比赛。我是以正确的方式接近这个吗?
PassportJS逻辑(req.user&& password === confirmPassword):
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var models = require('../app/models/db-index');
var bcrypt = require('bcrypt-nodejs');
//Sign Up Logic
passport.use('local-signup', new LocalStrategy({
passReqToCallback: true,
usernameField: 'email'
}, function(req, email, password, done){
models.User.findOne({
where: {
email: email
}
}).then(function(existingUser){
if (existingUser)
return done(null, false, req.flash('error', 'Email already exists.'));
if (req.user && password === confirmPassword) {
var user = req.user;
user.firstName = firstName;
user.lastName = lastName;
user.email = email;
user.password = models.User.generateHash(password);
user.save().catch(function(err){
throw err;
}).then(function(){
done(null, user, req.flash('error', 'All fields need to be filled in'));
});
} else {
var newUser = models.User.build({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: models.User.generateHash(password)
});
newUser.save().then(function(){
done(null, newUser);
}).catch(function(err){
done(null, false, console.log(err));
});
}
}).catch(function(e){
done(null, false, req.flash('error', 'All fields need to be filled in'),console.log(e.email + e.message));
})
}));
路线:
*==== /SIGN-UP ====*/
siteRoutes.route('/sign-up')
.get(function(req, res){
res.render('pages/site/sign-up.hbs',{
error: req.flash('error')
});
})
.post(passport.authenticate('local-signup', {
successRedirect: '/app/sign-up/organization',
failureRedirect: '/sign-up'
}));
查看:
<!DOCTYPE html>
<head>
{{> site/head}}
</head>
<body>
{{> site/navigation}}
<div class="container">
<div class="col-md-6 col-md-offset-3">
{{#if error}}
<div class="alert alert-danger">{{error}}</div>
{{/if}}
<h1>Sign Up</h1>
<form action="/sign-up" method="post">
<input type="text" class="form-control" id="sign-up-fist-name" name="firstName" value="" placeholder="First Name">
<br />
<input type="text" class="form-control" id="sign-up-username" name="lastName" value="" placeholder="Last Name">
<br />
<input type="text" class="form-control" id="sign-up-username" name="email" value="{{user.email}}" placeholder="Email">
<br />
<input type="password" class="form-control" id="sign-up-password" name="password" value="" placeholder="Password">
<br />
<input type="password" class="form-control" id="sign-up-password" name="confirmPassword" value="" placeholder="Confirm Password">
<button type="submit">Submit</button>
</form>
<a href="/login">Already have an account? Login here!</a>
</div>
</div>
</body>
答案 0 :(得分:0)
如果找不到解决方案,请使用javascript确认。这是我使用的最简单的方法。如果你找到解决方案,请分享她,我想用它。
<script>
function myFunction() {
var password = document.getElementById("password").value;
var password2 = document.getElementById("password2").value;
var ok = true;
if (password != password2) {
//$(".alert").alert();
//alert("Passwords Do not match");
document.getElementById("alert").style.display = 'block';
document.getElementById("password").style.borderColor = "#E34234";
document.getElementById("password2").style.borderColor = "#E34234";
ok = false;
} else {
alert("Passwords Match!!!");
}
return ok;
}
</script>