Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Users Module Revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
amoshaviv committed Mar 25, 2014
1 parent 994a220 commit 36f71fa
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 245 deletions.
174 changes: 152 additions & 22 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,29 @@
*/
var mongoose = require('mongoose'),
passport = require('passport'),
User = mongoose.model('User');
User = mongoose.model('User'),
_ = require('lodash');

var getErrorMessage = function(err) {
var message = '';

if (err.code) {
switch (err.code) {
case 11000:
case 11001:
message = 'Username already exists';
break;
default:
message = 'Something went wrong';
}
} else {
for (var errName in err.errors) {
if (err.errors[errName].message) message = err.errors[errName].message;
}
}

return message;
};

/**
* Signup
Expand All @@ -21,26 +43,22 @@ exports.signup = function(req, res) {

user.save(function(err) {
if (err) {
switch (err.code) {
case 11000:
case 11001:
message = 'Email or username already exists';
break;
default:
message = 'Please fill all the required fields';
}

return res.send(400, {
message: message
message: getErrorMessage(err)
});
} else {
// Remove sensitive data before login
user.password = undefined;
user.salt = undefined;

req.login(user, function(err) {
if (err) {
res.send(400, err);
} else {
res.jsonp(user);
}
});
}
req.logIn(user, function(err) {
if (err) {
res.send(400, err);
} else {
res.jsonp(user);
}
});
});
};

Expand All @@ -52,7 +70,11 @@ exports.signin = function(req, res, next) {
if (err || !user) {
res.send(400, info);
} else {
req.logIn(user, function(err) {
// Remove sensitive data before login
user.password = undefined;
user.salt = undefined;

req.login(user, function(err) {
if (err) {
res.send(400, err);
} else {
Expand All @@ -63,6 +85,98 @@ exports.signin = function(req, res, next) {
})(req, res, next);
};

/**
* Update user details
*/
exports.update = function(req, res) {
// Init Variables
var user = req.user;
var message = null;

if (user) {
// Merge existing user
user = _.extend(user, req.body);
user.updated = Date.now();
user.displayName = user.firstName + ' ' + user.lastName;

user.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.send(400, err);
} else {
res.jsonp(user);
}
});
}
});
} else {
res.send(400, {
message: 'User is not signed in'
});
}
};

/**
* Change Password
*/
exports.changePassword = function(req, res, next) {
// Init Variables
var passwordDetails = req.body;
var message = null;

if (req.user) {
User.findById(req.user.id, function(err, user) {
if (!err && user) {
if (user.authenticate(passwordDetails.currentPassword)) {
if (passwordDetails.newPassword === passwordDetails.verifyPassword) {
user.password = passwordDetails.newPassword;

user.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.send(400, err);
} else {
res.send({
message: 'Password changed successfully'
});
}
});
}
});

} else {
res.send(400, {
message: 'Passwords do not match'
});
}
} else {
res.send(400, {
message: 'Current password is incorrect'
});
}
} else {
res.send(400, {
message: 'User is not found'
});
}
});
} else {
res.send(400, {
message: 'User is not signed in'
});
}
};

/**
* Signout
*/
Expand All @@ -79,10 +193,24 @@ exports.me = function(req, res) {
};

/**
* Auth callback
* OAuth callback
*/
exports.authCallback = function(req, res) {
res.redirect('/');
exports.oauthCallback = function(strategy) {
return function(req, res, next) {
passport.authenticate(strategy, function(err, user, email) {
if (err || !user) {
console.log(err);
return res.redirect('/#!/signin');
}
req.login(user, function(err) {
if (err) {
return res.redirect('/#!/signin');
}

return res.redirect('/');
});
})(req, res, next);
};
};

/**
Expand All @@ -106,6 +234,7 @@ exports.requiresLogin = function(req, res, next) {
if (!req.isAuthenticated()) {
return res.send(401, 'User is not logged in');
}

next();
};

Expand All @@ -116,5 +245,6 @@ exports.hasAuthorization = function(req, res, next) {
if (req.profile.id !== req.user.id) {
return res.send(403, 'User is not authorized');
}

next();
};
Loading

0 comments on commit 36f71fa

Please sign in to comment.