#77 Fixed 500 after setting password, updated packages

master
Keith Irwin 2017-04-28 15:37:22 -04:00
parent 0f338b0d6f
commit 6758d0a7a1
No known key found for this signature in database
GPG Key ID: 378933C743E2BBC0
5 changed files with 41 additions and 37 deletions

View File

@ -44,6 +44,9 @@ Tracman will be updated according to [this branching model](http://nvie.com/post
#### v0.6.1
* [#77](https://github.com/Tracman-org/Server/issues/77) Fixed 500 after password change, swapped `bcrypt` for `bcrypt-nodejs`
* Removed extraneous packages
#### v0.6.0
* [#32](https://github.com/Tracman-org/Server/issues/32), [#57](https://github.com/Tracman-org/Server/issues/57), [#58](https://github.com/Tracman-org/Server/issues/58), [#60](https://github.com/Tracman-org/Server/issues/60) Added more login options

View File

@ -2,7 +2,7 @@
const mongoose = require('mongoose'),
unique = require('mongoose-unique-validator'),
bcrypt = require('bcrypt-nodejs'),
bcrypt = require('bcrypt'),
crypto = require('crypto');
const userSchema = new mongoose.Schema({
@ -111,18 +111,31 @@ const userSchema = new mongoose.Schema({
};
// Generate hash for new password
userSchema.methods.generateHash = function(password,next){
// next(err,hash);
bcrypt.genSalt(8)
.then( (salt)=>{
bcrypt.hash(password, salt, null, next);
})
.catch( (err)=>{ return next(err,null); });
// Generate hash for new password and save it to the database
userSchema.methods.generateHashedPassword = function(password,next){
// next(err);
// Delete token
this.auth.passToken = undefined;
this.auth.passTokenExpires = undefined;
// Generate hash
bcrypt.genSalt(8, (err,salt)=>{
if (err){ return next(err); }
bcrypt.hash(password, salt, (err,hash)=>{
if (err){ return next(err); }
this.auth.password = hash;
this.save();
next();
});
});
};
// Check for valid password
userSchema.methods.validPassword = function(password,next){
// next(err,res);
// res = true/false
bcrypt.compare(password, this.auth.password, next);
};

View File

@ -301,31 +301,24 @@ router.route('/password/:token')
else {
// Delete token
res.locals.passwordUser.auth.passToken = undefined;
res.locals.passwordUser.auth.passTokenExpires = undefined;
// Create hash
res.locals.passwordUser.generateHash( req.body.password, (err,hash)=>{
// Create hashed password and save to db
res.locals.passwordUser.generateHashedPassword( req.body.password, (err)=>{
if (err){
mw.throwErr(err,req);
res.redirect(`/password/${req.params.token}`);
}
else {
// Save new password to db
res.locals.passwordUser.auth.password = hash;
res.locals.passwordUser.save()
.then( ()=>{
req.flash('success', 'Password set. You can use it to log in now. ');
res.redirect('/login#login');
})
.catch( (err)=>{
mw.throwErr(err,req);
res.redirect('/login#signup');
});
// User changed password
else if (req.user) {
req.flash('success', 'Your password has been changed. ');
res.redirect('/settings');
}
// New user created password
else {
req.flash('success', 'Password set. You can use it to log in now. ');
res.redirect('/login#login');
}
} );
}

View File

@ -4,22 +4,19 @@
"description": "Tracks user's GPS location",
"main": "server.js",
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"bcrypt": "^1.0.2",
"body-parser": "^1.17.1",
"connect-flash": "^0.1.1",
"connect-flash-plus": "^0.2.1",
"cookie-parser": "^1.4.1",
"cookie-session": "^2.0.0-alpha.1",
"express": "^4.15.2",
"express-validator": "^3.1.3",
"firebase": "^3.7.2",
"kerberos": "0.0.17",
"mellt": "^1.0.0",
"moment": "^2.12.0",
"mongodb": "^2.1.4",
"mongodb": "^2.2.26",
"mongoose": "^4.9.0",
"mongoose-unique-validator": "^1.0.5",
"node-jose": "^0.8.0",
"nodemailer": "^3.1.8",
"nunjucks": "^2.3.0",
"passport": "^0.3.2",

View File

@ -143,9 +143,7 @@ const
// Development handlers
else {
app.use( (err,req,res,next)=>{
if (err.status!==404) {
console.error(`${err.stack}`);
}
if (err.status!==404) { console.error(`${err.stack}`); }
if (res.headersSent) { return next(err); }
res.status(err.status||500);
res.render('error', {