fix login and signup bugs
This commit is contained in:
parent
ab193a0271
commit
e20b0545d1
3 changed files with 27 additions and 32 deletions
|
@ -41,15 +41,16 @@ function validate(formProps) {
|
||||||
errors.confirmPassword = 'Please enter a password confirmation';
|
errors.confirmPassword = 'Please enter a password confirmation';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formProps.password !== formProps.passwordConfirm) {
|
if (formProps.password !== formProps.confirmPassword) {
|
||||||
errors.password = 'Passwords must match';
|
errors.password = 'Passwords must match';
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors;
|
return errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
// export default connect(mapStateToProps, mapDispatchToProps)(SignupView);
|
// export default connect(mapStateToProps, mapDispatchToProps)(SignupView);
|
||||||
export default reduxForm({
|
export default reduxForm({
|
||||||
form: 'signup',
|
form: 'signup',
|
||||||
fields: ['username', 'email', 'password', 'passwordConfirm'],
|
fields: ['username', 'email', 'password', 'confirmPassword'],
|
||||||
validate
|
validate
|
||||||
}, mapStateToProps, mapDispatchToProps)(SignupView);
|
}, mapStateToProps, mapDispatchToProps)(SignupView);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
const passport = require('passport');
|
const passport = require('passport');
|
||||||
// const GitHubStrategy = require('passport-github').Strategy;
|
const GitHubStrategy = require('passport-github').Strategy;
|
||||||
const LocalStrategy = require('passport-local').Strategy;
|
const LocalStrategy = require('passport-local').Strategy;
|
||||||
|
|
||||||
import User from '../models/user';
|
import User from '../models/user'
|
||||||
|
|
||||||
passport.serializeUser((user, done) => {
|
passport.serializeUser((user, done) => {
|
||||||
done(null, user.id);
|
done(null, user.id);
|
||||||
|
@ -18,15 +18,11 @@ passport.deserializeUser((id, done) => {
|
||||||
* Sign in using Email and Password.
|
* Sign in using Email and Password.
|
||||||
*/
|
*/
|
||||||
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
|
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
|
||||||
User.findOne({ email: email.toLowerCase() },
|
User.findOne({ email: email.toLowerCase() }, (err, user) => {
|
||||||
(err, user) => { // eslint-disable-line consistent-return
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return done(null, false, { msg: `Email ${email} not found.` });
|
return done(null, false, { msg: `Email ${email} not found.` });
|
||||||
}
|
}
|
||||||
user.comparePassword(password, (innerErr, isMatch) => {
|
user.comparePassword(password, (err, isMatch) => {
|
||||||
if (innerErr) {
|
|
||||||
return done(innerErr);
|
|
||||||
}
|
|
||||||
if (isMatch) {
|
if (isMatch) {
|
||||||
return done(null, user);
|
return done(null, user);
|
||||||
}
|
}
|
||||||
|
@ -48,8 +44,7 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, don
|
||||||
// if (req.user) {
|
// if (req.user) {
|
||||||
// User.findOne({ github: profile.id }, (err, existingUser) => {
|
// User.findOne({ github: profile.id }, (err, existingUser) => {
|
||||||
// if (existingUser) {
|
// if (existingUser) {
|
||||||
// req.flash('errors', { msg: 'There is already a GitHub account that belongs to you. '
|
// req.flash('errors', { msg: 'There is already a GitHub account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
|
||||||
// + 'Sign in with that account or delete it, then link it with your current account.' });
|
|
||||||
// done(err);
|
// done(err);
|
||||||
// } else {
|
// } else {
|
||||||
// User.findById(req.user.id, (err, user) => {
|
// User.findById(req.user.id, (err, user) => {
|
||||||
|
@ -73,8 +68,7 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, don
|
||||||
// }
|
// }
|
||||||
// User.findOne({ email: profile._json.email }, (err, existingEmailUser) => {
|
// User.findOne({ email: profile._json.email }, (err, existingEmailUser) => {
|
||||||
// if (existingEmailUser) {
|
// if (existingEmailUser) {
|
||||||
// req.flash('errors', { msg: 'There is already an account using this email address. Sign'
|
// req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with GitHub manually from Account Settings.' });
|
||||||
// + ' in to that account and link it with GitHub manually from Account Settings.' });
|
|
||||||
// done(err);
|
// done(err);
|
||||||
// } else {
|
// } else {
|
||||||
// const user = new User();
|
// const user = new User();
|
||||||
|
|
|
@ -15,15 +15,15 @@ const userSchema = new Schema({
|
||||||
/**
|
/**
|
||||||
* Password hash middleware.
|
* Password hash middleware.
|
||||||
*/
|
*/
|
||||||
userSchema.pre('save', (next) => { // eslint-disable-line consistent-return
|
userSchema.pre('save', function (next) {
|
||||||
const user = this;
|
const user = this;
|
||||||
if (!user.isModified('password')) { return next(); }
|
if (!user.isModified('password')) { return next(); }
|
||||||
bcrypt.genSalt(10, (err, salt) => { // eslint-disable-line consistent-return
|
bcrypt.genSalt(10, (err, salt) => {
|
||||||
|
if (err) { return next(err); }
|
||||||
|
bcrypt.hash(user.password, salt, null, (err, hash) => {
|
||||||
if (err) { return next(err); }
|
if (err) { return next(err); }
|
||||||
bcrypt.hash(user.password, salt, null, (innerErr, hash) => {
|
|
||||||
if (innerErr) { return next(innerErr); }
|
|
||||||
user.password = hash;
|
user.password = hash;
|
||||||
return next();
|
next();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -31,7 +31,7 @@ userSchema.pre('save', (next) => { // eslint-disable-line consistent-return
|
||||||
/**
|
/**
|
||||||
* Helper method for validating user's password.
|
* Helper method for validating user's password.
|
||||||
*/
|
*/
|
||||||
userSchema.methods.comparePassword = (candidatePassword, cb) => {
|
userSchema.methods.comparePassword = function (candidatePassword, cb) {
|
||||||
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
|
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
|
||||||
cb(err, isMatch);
|
cb(err, isMatch);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue