Skip to content

Commit

Permalink
Refine feedback message and add Check for unique email field for user
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzwen committed Feb 7, 2017
1 parent 48f40c1 commit 51a03af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion backend/controllers/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.postClients = function (req, res) {
// Save the client and check for errors
Client.build({name: name, id: id, secret: secret, userId: userId})
.save().then(function (success) {
res.json({message: 'Client created!'});
res.json({status: 'Success', message: 'Client Created'});
});
};

Expand Down
48 changes: 24 additions & 24 deletions backend/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
var User = require('../models/user.js');
var crypto = require('crypto');

var add = function (user, onSuccess, onError) {
var password = user.password;

var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');

User.build({email: user.email, username: user.username, password: password})
.save().then(onSuccess).catch(onError);
};

exports.postUser = function (req, res) {
var username = req.body.username;
var password = req.body.password;
var email = req.body.email;

var user = User.build({email: email, username: username, password: password});
var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');

add(user, function (success) {
res.json({message: 'User created!'});
},
function (err) {
User.find({where: {email: email}})
.then(function (users) {
if (users) {
return res.json({status: 'fail', message: 'User Existed'});
}
});

User.build({email: email, username: username, password: password})
.save()
.then(function () {
res.json({status: 'success', message: 'User Created'});
})
.catch(function (err) {
res.send(err);
});
};

var retrieveAll = function (onSuccess, onError) {
User.findAll({}, {raw: true}).then(onSuccess).catch(onError);
};

exports.getUser = function (req, res) {
retrieveAll(function (users) {
if (users) {
Expand All @@ -39,16 +43,12 @@ exports.getUser = function (req, res) {
});
};

var retrieveAll = function (onSuccess, onError) {
User.findAll({}, {raw: true}).then(onSuccess).catch(onError);
};

exports.retrieveById = function (user_id, onSuccess, onError) {
User.find({where: {id: user_id}}, {raw: true}).then(onSuccess).catch(onError);
exports.retrieveById = function (userId, onSuccess, onError) {
User.find({where: {id: userId}}, {raw: true}).then(onSuccess).catch(onError);
};

exports.updateById = function (user, user_id, onSuccess, onError) {
var id = user_id;
exports.updateById = function (user, userId, onSuccess, onError) {
var id = userId;
var username = user.username;
var password = user.password;

Expand Down

0 comments on commit 51a03af

Please sign in to comment.