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'; 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);

View file

@ -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);
} }
@ -38,7 +34,7 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, don
/** /**
* Sign in with GitHub. * 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({ // passport.use(new GitHubStrategy({
// clientID: process.env.GITHUB_ID, // clientID: process.env.GITHUB_ID,
// clientSecret: process.env.GITHUB_SECRET, // clientSecret: process.env.GITHUB_SECRET,
@ -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();

View file

@ -4,26 +4,26 @@ const bcrypt = require('bcrypt-nodejs');
const userSchema = new Schema({ const userSchema = new Schema({
name: { type: String, default: '' }, name: { type: String, default: '' },
username: { type: String, required: true, unique: true }, username: { type: String, required: true, unique: true},
password: { type: String }, password: { type: String },
github: { type: String }, github: { type: String },
email: { type: String, unique: true }, email: { type: String, unique: true },
tokens: Array, tokens: Array,
admin: { type: Boolean, default: false } admin: { type: Boolean, default: false }
}, { timestamps: true }); }, {timestamps: true});
/** /**
* 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);
}); });