Skip to content

Commit

Permalink
fixed specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jonniespratley committed Dec 9, 2014
1 parent 035124e commit 9f8da4a
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions routes/cms-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,18 @@ module.exports = function (config, app) {
*/
login: function (req, res, next) {
var query = {};

//TODO: Need to make this externalized.
if (req.body.username) {
query.username = req.body.username;
}
if (req.body.email) {
query.username = req.body.email;
}

//TODO: Hashing on client side
query.password = hashPassword( req.body.password, query.username );

console.warn( 'Login Query: ' + JSON.stringify( query ) + ''.verbose );

User.findOne( {username: query.username}, function (err, data) {
if (err) {
res.jsonp( 400, err );
}
if (data) {
console.warn('found user', util.inspect(data, {colors: true}));
if (data && bcrypt.compareSync(req.body.password, data.password)) {
res.jsonp( 200, data );
} else {
res.jsonp( 404, {message: 'Wrong username/password!'} );
Expand All @@ -75,10 +67,7 @@ module.exports = function (config, app) {
if (req.body.email) {
data.username = req.body.email;
}

//TODO: Hashing on client side
data.password = hashPassword( req.body.password, data.username );

data.created_at = new Date();
data.updated_at = new Date();
data.active = false;
Expand All @@ -89,12 +78,16 @@ module.exports = function (config, app) {
//Try and find user
User.find( {username: data.username}, function (err, u) {
console.log(err, util.inspect(u, {colors: true}));

if(err){
res.json( 400, {message: 'Problem registering!'} );
}

if (u.length > 0) {
res.json( 400, {message: 'Username already exists!'} );
} else {
}

if(!err){
user.save( function (er, ok) {
if (er) {
res.json( 400, {message: 'Problem registering!'} );
Expand All @@ -110,6 +103,11 @@ module.exports = function (config, app) {
};

//Always users table
app.use( session( {
secret: 'angular-cms',
resave: true,
saveUninitialized: true
} ) );
app.post( config.apiBase + '/users/login', bodyParser.json(), cmsAuth.login );
app.post( config.apiBase + '/users/register', bodyParser.json(), cmsAuth.register );
app.post( config.apiBase + '/users/session', bodyParser.json(), cmsAuth.session );
Expand Down

0 comments on commit 9f8da4a

Please sign in to comment.