Merge pull request #3597 from 3onyc/feature/username-change

Implement "Change username" feature
This commit is contained in:
Tyler Renelle
2014-07-19 16:48:20 -06:00
4 changed files with 52 additions and 0 deletions
+25
View File
@@ -195,6 +195,31 @@ api.resetPassword = function(req, res, next){
});
};
api.changeUsername = function(req, res, next) {
var user = res.locals.user,
password = req.body.password
newUsername = req.body.newUsername;
User.findOne({'auth.local.username': newUsername}, function(err, result) {
if (err) next(err);
if(result)
return res.json(401, {err: "Username already taken"});
var salt = user.auth.local.salt,
hashed_password = utils.encryptPassword(password, salt);
if (hashed_password !== user.auth.local.hashed_password)
return res.json(401, {err:"Incorrect password"});
user.auth.local.username = newUsername;
user.save(function(err, saved){
if (err) next(err);
res.send(200);
})
});
}
api.changePassword = function(req, res, next) {
var user = res.locals.user,
oldPassword = req.body.oldPassword,
+1
View File
@@ -10,6 +10,7 @@ router.post('/api/v2/user/auth/local', i18n.getUserLanguage, auth.loginLocal);
router.post('/api/v2/user/auth/facebook', i18n.getUserLanguage, auth.loginFacebook);
router.post('/api/v2/user/reset-password', i18n.getUserLanguage, auth.resetPassword);
router.post('/api/v2/user/change-password', i18n.getUserLanguage, auth.auth, auth.changePassword);
router.post('/api/v2/user/change-username', i18n.getUserLanguage, auth.auth, auth.changeUsername);
router.post('/api/v1/register', i18n.getUserLanguage, auth.registerUser);
router.post('/api/v1/user/auth/local', i18n.getUserLanguage, auth.loginLocal);