Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User authentication / registration, fixture fix #85

Merged
merged 1 commit into from
May 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
connection: {
filename: './core/shared/data/testdb.db'
},
debug: true
debug: false
// debug: true
},

staging: {},
Expand Down
17 changes: 10 additions & 7 deletions core/admin/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@
console.log('user found: ', user);
req.session.user = "ghostadmin";
res.redirect(req.query.redirect || '/ghost/');
}, function (err) {
}, function (error) {
// Do something here to signal the reason for an error
console.log(err.stack);
req.flash('error', error.message);
res.redirect('/ghost/login/');
});
},
Expand All @@ -78,16 +78,19 @@
});
},
'doRegister': function (req, res) {
// console.log(req.body);
if (req.body.email_address !== '' && req.body.password.length > 5) {
var email = req.body.email_address,
password = req.body.password;

if (email !== '' && password.length > 5) {
api.users.add({
email_address: req.body.email_address,
password: req.body.password
email_address: email,
password: password
}).then(function (user) {
console.log('user added', user);
res.redirect('/ghost/login/');
}, function (error) {
console.log('there was an error', error);
req.flash('error', error.message);
res.redirect('/ghost/register/');
});
} else {
req.flash('error', "The password is too short. Have at least 6 characters in there");
Expand Down
9 changes: 5 additions & 4 deletions core/shared/data/fixtures/001.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 18 additions & 9 deletions core/shared/models/dataProvider.bookshelf.users.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@
// Clone the _user so we don't expose the hashed password unnecessarily
userData = _.extend({}, _user);

return nodefn.call(bcrypt.hash, _user.password, null, null).then(function (hash) {
userData.password = hash;
return BaseProvider.prototype.add.call(self, userData);
return self.model.forge({email_address: userData.email_address}).fetch().then(function (user) {
if (!!user.attributes.email_address) {
return when.reject(new Error('A user with that email address already exists.'));
}

return nodefn.call(bcrypt.hash, _user.password, null, null).then(function (hash) {
userData.password = hash;
return BaseProvider.prototype.add.call(self, userData);
});
});

};

/**
Expand All @@ -47,12 +53,15 @@
return this.model.forge({
email_address: _userdata.email
}).fetch().then(function (user) {
return nodefn.call(bcrypt.compare, _userdata.pw, user.get('password')).then(function (matched) {
if (!matched) {
return when.reject(new Error('Password does not match'));
}
return user;
});
if (!!user.attributes.email_address) {
return nodefn.call(bcrypt.compare, _userdata.pw, user.get('password')).then(function (matched) {
if (!matched) {
return when.reject(new Error('Passwords do not match'));
}
return user;
});
}
return when.reject(new Error('We do not have a record for such user.'));
});
};

Expand Down