fix login and signup bugs

This commit is contained in:
catarak 2016-06-24 18:08:52 -04:00
parent ab193a0271
commit e20b0545d1
3 changed files with 27 additions and 32 deletions

View file

@ -41,15 +41,16 @@ function validate(formProps) {
errors.confirmPassword = 'Please enter a password confirmation';
}
if (formProps.password !== formProps.passwordConfirm) {
if (formProps.password !== formProps.confirmPassword) {
errors.password = 'Passwords must match';
}
return errors;
}
// export default connect(mapStateToProps, mapDispatchToProps)(SignupView);
export default reduxForm({
form: 'signup',
fields: ['username', 'email', 'password', 'passwordConfirm'],
fields: ['username', 'email', 'password', 'confirmPassword'],
validate
}, mapStateToProps, mapDispatchToProps)(SignupView);

View file

@ -1,8 +1,8 @@
const passport = require('passport');
// const GitHubStrategy = require('passport-github').Strategy;
const GitHubStrategy = require('passport-github').Strategy;
const LocalStrategy = require('passport-local').Strategy;
import User from '../models/user';
import User from '../models/user'
passport.serializeUser((user, done) => {
done(null, user.id);
@ -18,27 +18,23 @@ passport.deserializeUser((id, done) => {
* Sign in using Email and Password.
*/
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
User.findOne({ email: email.toLowerCase() },
(err, user) => { // eslint-disable-line consistent-return
if (!user) {
return done(null, false, { msg: `Email ${email} not found.` });
User.findOne({ email: email.toLowerCase() }, (err, user) => {
if (!user) {
return done(null, false, { msg: `Email ${email} not found.` });
}
user.comparePassword(password, (err, isMatch) => {
if (isMatch) {
return done(null, user);
}
user.comparePassword(password, (innerErr, isMatch) => {
if (innerErr) {
return done(innerErr);
}
if (isMatch) {
return done(null, user);
}
return done(null, false, { msg: 'Invalid email or password.' });
});
return done(null, false, { msg: 'Invalid email or password.' });
});
});
}));
/**
* Sign in with GitHub.
*/
// TODO add dotenv so I can add github login
//TODO add dotenv so I can add github login
// passport.use(new GitHubStrategy({
// clientID: process.env.GITHUB_ID,
// clientSecret: process.env.GITHUB_SECRET,
@ -48,8 +44,7 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, don
// if (req.user) {
// User.findOne({ github: profile.id }, (err, existingUser) => {
// if (existingUser) {
// 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.' });
// 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.' });
// done(err);
// } else {
// 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) => {
// if (existingEmailUser) {
// 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.' });
// 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.' });
// done(err);
// } else {
// const user = new User();

View file

@ -4,26 +4,26 @@ const bcrypt = require('bcrypt-nodejs');
const userSchema = new Schema({
name: { type: String, default: '' },
username: { type: String, required: true, unique: true },
username: { type: String, required: true, unique: true},
password: { type: String },
github: { type: String },
email: { type: String, unique: true },
tokens: Array,
admin: { type: Boolean, default: false }
}, { timestamps: true });
}, {timestamps: true});
/**
* Password hash middleware.
*/
userSchema.pre('save', (next) => { // eslint-disable-line consistent-return
userSchema.pre('save', function (next) {
const user = this;
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, (innerErr, hash) => {
if (innerErr) { return next(innerErr); }
bcrypt.hash(user.password, salt, null, (err, hash) => {
if (err) { return next(err); }
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.
*/
userSchema.methods.comparePassword = (candidatePassword, cb) => {
userSchema.methods.comparePassword = function (candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
cb(err, isMatch);
});