From e20b0545d1f34e584854fde136db567999e77c20 Mon Sep 17 00:00:00 2001 From: catarak Date: Fri, 24 Jun 2016 18:08:52 -0400 Subject: [PATCH] fix login and signup bugs --- client/modules/User/pages/SignupView.js | 5 ++-- server/config/passport.js | 36 +++++++++++-------------- server/models/user.js | 18 ++++++------- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/client/modules/User/pages/SignupView.js b/client/modules/User/pages/SignupView.js index e24283c8..e94c4ba3 100644 --- a/client/modules/User/pages/SignupView.js +++ b/client/modules/User/pages/SignupView.js @@ -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); diff --git a/server/config/passport.js b/server/config/passport.js index 65cb2b94..d855dad4 100644 --- a/server/config/passport.js +++ b/server/config/passport.js @@ -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(); @@ -92,4 +86,4 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, don // }); // }); // } -// })); +// })); \ No newline at end of file diff --git a/server/models/user.js b/server/models/user.js index 30259775..6294a9fb 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -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,10 +31,10 @@ 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); }); }; -export default mongoose.model('User', userSchema); +export default mongoose.model('User', userSchema); \ No newline at end of file