{"action":"created","comment":{"url":"https://api.github.com/repos/andela/banasko-cfh/pulls/comments/91855276","pull_request_review_id":12366243,"id":91855276,"diff_hunk":"@@ -7,66 +7,65 @@ const config = require('../../config/config');\n const validate = require('validator');\n const User = mongoose.model('User');\n \n-exports.signUp = (req, res) => {\n-  const name = req.body.name;\n-  const username = req.body.username;\n-  const email = req.body.email;\n-  const pwd = req.body.password;\n-  const avatar = req.body.avatar;\n+const errorHandler = (res, message, status) => {\n+  return res.status(status).json({\n+    success: false,\n+    message: message\n+  })\n+}\n \n-  User.findOne({\n-    email: email\n-  }, function (err, registerUser) {\n-    if (err) {\n-      res.send(err);\n-    }\n-    if (!registerUser) {\n-      if (name && email && pwd) {\n-        if ((pwd.trim().length) < 8 || !validate.isEmail(email) ||\n-          !validate.isAlpha(name)) {\n-          res.status(400).json({\n-            success: false,\n-            message: 'Invalid details provided.'\n-          });\n-        } else {\n-          let user = new User({\n-            name: name,\n-            username: username,\n-            email: email,\n-            password: pwd,\n-            avatar: avatar\n-          });\n+exports.signUp = (req, res) => {\n+  const body = req.body;\n+  if (body.username && body.name && body.password && body.email) {\n+    if ((body.password.trim().length > 8) && validate.isEmail(body.email) && validate.isAlpha(body.username) &&\n+      !validate.isEmpty(body.name)) {\n+      User.findOne({\n+        email: body.email\n+      }, (err, user) => {\n+        if (!user) {\n+          User.findOne({\n+            username: body.username\n+          }, (err, registerUser) => {\n+            if (!registerUser) {\n+              let user = new User({\n+                name: body.name,\n+                username: body.username,\n+                email: body.email,\n+                password: body.password,\n+                avatar: body.avatar\n+              });\n+              user.save((err, saveUser) => {\n+                if (err) {\n+                  res.send(err);\n+                }\n+                let token = jwt.sign({\n+                  userId: saveUser._id,\n+                  expiresInMinutes: moments().day(7)\n+                }, config.secret);\n+                res.status(201).json({\n+                  success: true,\n+                  message: 'Thank you for signing up!',\n+                  token: token\n+                });\n+              });\n \n-          user.save((err, saveUser) => {\n-            if (err) {\n-              res.send(err);\n+            } else {\n+              errorHandler(res, \"This username is taken!\", 409);\n             }\n-            let token = jwt.sign({\n-              userId: saveUser._id,\n-              expiresInMinutes: moments().day(7)\n-            }, config.secret);\n-            res.status(201).json({\n-              success: true,\n-              message: 'Thank you for signing up!',\n-              token: token\n-            });\n-          });\n+          })\n+        } else {\n+          errorHandler(res, \"This email already exists!\", 409);","path":"app/controllers/auth.js","position":92,"original_position":92,"commit_id":"e19eb913b6d1fbcebd47c99e2dd4cd38b2d59b82","original_commit_id":"e19eb913b6d1fbcebd47c99e2dd4cd38b2d59b82","user":{"login":"houndci-bot","id":6697940,"avatar_url":"https://avatars.githubusercontent.com/u/6697940?v=3","gravatar_id":"","url":"https://api.github.com/users/houndci-bot","html_url":"https://github.com/houndci-bot","followers_url":"https://api.github.com/users/houndci-bot/followers","following_url":"https://api.github.com/users/houndci-bot/following{/other_user}","gists_url":"https://api.github.com/users/houndci-bot/gists{/gist_id}","starred_url":"https://api.github.com/users/houndci-bot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/houndci-bot/subscriptions","organizations_url":"https://api.github.com/users/houndci-bot/orgs","repos_url":"https://api.github.com/users/houndci-bot/repos","events_url":"https://api.github.com/users/houndci-bot/events{/privacy}","received_events_url":"https://api.github.com/users/houndci-bot/received_events","type":"User","site_admin":false},"body":"Mixed double and single quotes.","created_at":"2016-12-11T12:58:35Z","updated_at":"2016-12-11T12:58:35Z","html_url":"https://github.com/andela/banasko-cfh/pull/13#discussion_r91855276","pull_request_url":"https://api.github.com/repos/andela/banasko-cfh/pulls/13","_links":{"self":{"href":"https://api.github.com/repos/andela/banasko-cfh/pulls/comments/91855276"},"html":{"href":"https://github.com/andela/banasko-cfh/pull/13#discussion_r91855276"},"pull_request":{"href":"https://api.github.com/repos/andela/banasko-cfh/pulls/13"}}},"pull_request":{"url":"https://api.github.com/repos/andela/banasko-cfh/pulls/13","id":97458155,"html_url":"https://github.com/andela/banasko-cfh/pull/13","diff_url":"https://github.com/andela/banasko-cfh/pull/13.diff","patch_url":"https://github.com/andela/banasko-cfh/pull/13.patch","issue_url":"https://api.github.com/repos/andela/banasko-cfh/issues/13","number":13,"state":"open","locked":false,"title":"#134249131 A user can not signup with an existing username. ","user":{"login":"andela-sayidu","id":23451229,"avatar_url":"https://avatars.githubusercontent.com/u/23451229?v=3","gravatar_id":"","url":"https://api.github.com/users/andela-sayidu","html_url":"https://github.com/andela-sayidu","followers_url":"https://api.github.com/users/andela-sayidu/followers","following_url":"https://api.github.com/users/andela-sayidu/following{/other_user}","gists_url":"https://api.github.com/users/andela-sayidu/gists{/gist_id}","starred_url":"https://api.github.com/users/andela-sayidu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andela-sayidu/subscriptions","organizations_url":"https://api.github.com/users/andela-sayidu/orgs","repos_url":"https://api.github.com/users/andela-sayidu/repos","events_url":"https://api.github.com/users/andela-sayidu/events{/privacy}","received_events_url":"https://api.github.com/users/andela-sayidu/received_events","type":"User","site_admin":false},"body":"#### What does this PR do?            \r\nThis PR addresses issues with the current JWT signup validation, which only ensures that email are unique at this stage. However, as usernames should be unique as well, the validation system was updated to take this into consideration.   \r\n               \r\n#### How should this be manually tested?   \r\nThis can be manually tested by using POSTMAN to send data to the signup route. \r\n                          ","created_at":"2016-12-11T12:58:29Z","updated_at":"2016-12-11T12:58:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":"483601ca2583aad53fa9bb0dd120533f904594b3","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/andela/banasko-cfh/pulls/13/commits","review_comments_url":"https://api.github.com/repos/andela/banasko-cfh/pulls/13/comments","review_comment_url":"https://api.github.com/repos/andela/banasko-cfh/pulls/comments{/number}","comments_url":"https://api.github.com/repos/andela/banasko-cfh/issues/13/comments","statuses_url":"https://api.github.com/repos/andela/banasko-cfh/statuses/e19eb913b6d1fbcebd47c99e2dd4cd38b2d59b82","head":{"label":"andela:bugfix/134249131/username-for-jwt-signup","ref":"bugfix/134249131/username-for-jwt-signup","sha":"e19eb913b6d1fbcebd47c99e2dd4cd38b2d59b82","user":{"login":"andela","id":4100206,"avatar_url":"https://avatars.githubusercontent.com/u/4100206?v=3","gravatar_id":"","url":"https://api.github.com/users/andela","html_url":"https://github.com/andela","followers_url":"https://api.github.com/users/andela/followers","following_url":"https://api.github.com/users/andela/following{/other_user}","gists_url":"https://api.github.com/users/andela/gists{/gist_id}","starred_url":"https://api.github.com/users/andela/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andela/subscriptions","organizations_url":"https://api.github.com/users/andela/orgs","repos_url":"https://api.github.com/users/andela/repos","events_url":"https://api.github.com/users/andela/events{/privacy}","received_events_url":"https://api.github.com/users/andela/received_events","type":"Organization","site_admin":false},"repo":{"id":73644823,"name":"banasko-cfh","full_name":"andela/banasko-cfh","owner":{"login":"andela","id":4100206,"avatar_url":"https://avatars.githubusercontent.com/u/4100206?v=3","gravatar_id":"","url":"https://api.github.com/users/andela","html_url":"https://github.com/andela","followers_url":"https://api.github.com/users/andela/followers","following_url":"https://api.github.com/users/andela/following{/other_user}","gists_url":"https://api.github.com/users/andela/gists{/gist_id}","starred_url":"https://api.github.com/users/andela/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andela/subscriptions","organizations_url":"https://api.github.com/users/andela/orgs","repos_url":"https://api.github.com/users/andela/repos","events_url":"https://api.github.com/users/andela/events{/privacy}","received_events_url":"https://api.github.com/users/andela/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/andela/banasko-cfh","description":null,"fork":false,"url":"https://api.github.com/repos/andela/banasko-cfh","forks_url":"https://api.github.com/repos/andela/banasko-cfh/forks","keys_url":"https://api.github.com/repos/andela/banasko-cfh/keys{/key_id}","collaborators_url":"https://api.github.com/repos/andela/banasko-cfh/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/andela/banasko-cfh/teams","hooks_url":"https://api.github.com/repos/andela/banasko-cfh/hooks","issue_events_url":"https://api.github.com/repos/andela/banasko-cfh/issues/events{/number}","events_url":"https://api.github.com/repos/andela/banasko-cfh/events","assignees_url":"https://api.github.com/repos/andela/banasko-cfh/assignees{/user}","branches_url":"https://api.github.com/repos/andela/banasko-cfh/branches{/branch}","tags_url":"https://api.github.com/repos/andela/banasko-cfh/tags","blobs_url":"https://api.github.com/repos/andela/banasko-cfh/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/andela/banasko-cfh/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/andela/banasko-cfh/git/refs{/sha}","trees_url":"https://api.github.com/repos/andela/banasko-cfh/git/trees{/sha}","statuses_url":"https://api.github.com/repos/andela/banasko-cfh/statuses/{sha}","languages_url":"https://api.github.com/repos/andela/banasko-cfh/languages","stargazers_url":"https://api.github.com/repos/andela/banasko-cfh/stargazers","contributors_url":"https://api.github.com/repos/andela/banasko-cfh/contributors","subscribers_url":"https://api.github.com/repos/andela/banasko-cfh/subscribers","subscription_url":"https://api.github.com/repos/andela/banasko-cfh/subscription","commits_url":"https://api.github.com/repos/andela/banasko-cfh/commits{/sha}","git_commits_url":"https://api.github.com/repos/andela/banasko-cfh/git/commits{/sha}","comments_url":"https://api.github.com/repos/andela/banasko-cfh/comments{/number}","issue_comment_url":"https://api.github.com/repos/andela/banasko-cfh/issues/comments{/number}","contents_url":"https://api.github.com/repos/andela/banasko-cfh/contents/{+path}","compare_url":"https://api.github.com/repos/andela/banasko-cfh/compare/{base}...{head}","merges_url":"https://api.github.com/repos/andela/banasko-cfh/merges","archive_url":"https://api.github.com/repos/andela/banasko-cfh/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/andela/banasko-cfh/downloads","issues_url":"https://api.github.com/repos/andela/banasko-cfh/issues{/number}","pulls_url":"https://api.github.com/repos/andela/banasko-cfh/pulls{/number}","milestones_url":"https://api.github.com/repos/andela/banasko-cfh/milestones{/number}","notifications_url":"https://api.github.com/repos/andela/banasko-cfh/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/andela/banasko-cfh/labels{/name}","releases_url":"https://api.github.com/repos/andela/banasko-cfh/releases{/id}","deployments_url":"https://api.github.com/repos/andela/banasko-cfh/deployments","created_at":"2016-11-13T21:42:09Z","updated_at":"2016-12-02T13:39:19Z","pushed_at":"2016-12-11T12:58:29Z","git_url":"git://github.com/andela/banasko-cfh.git","ssh_url":"git@github.com:andela/banasko-cfh.git","clone_url":"https://github.com/andela/banasko-cfh.git","svn_url":"https://github.com/andela/banasko-cfh","homepage":"https://banaskotest.herokuapp.com/#!/","size":10845,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"development"}},"base":{"label":"andela:development","ref":"development","sha":"03d2b1e5af6e8957b82890b6cae4d980155422f1","user":{"login":"andela","id":4100206,"avatar_url":"https://avatars.githubusercontent.com/u/4100206?v=3","gravatar_id":"","url":"https://api.github.com/users/andela","html_url":"https://github.com/andela","followers_url":"https://api.github.com/users/andela/followers","following_url":"https://api.github.com/users/andela/following{/other_user}","gists_url":"https://api.github.com/users/andela/gists{/gist_id}","starred_url":"https://api.github.com/users/andela/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andela/subscriptions","organizations_url":"https://api.github.com/users/andela/orgs","repos_url":"https://api.github.com/users/andela/repos","events_url":"https://api.github.com/users/andela/events{/privacy}","received_events_url":"https://api.github.com/users/andela/received_events","type":"Organization","site_admin":false},"repo":{"id":73644823,"name":"banasko-cfh","full_name":"andela/banasko-cfh","owner":{"login":"andela","id":4100206,"avatar_url":"https://avatars.githubusercontent.com/u/4100206?v=3","gravatar_id":"","url":"https://api.github.com/users/andela","html_url":"https://github.com/andela","followers_url":"https://api.github.com/users/andela/followers","following_url":"https://api.github.com/users/andela/following{/other_user}","gists_url":"https://api.github.com/users/andela/gists{/gist_id}","starred_url":"https://api.github.com/users/andela/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andela/subscriptions","organizations_url":"https://api.github.com/users/andela/orgs","repos_url":"https://api.github.com/users/andela/repos","events_url":"https://api.github.com/users/andela/events{/privacy}","received_events_url":"https://api.github.com/users/andela/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/andela/banasko-cfh","description":null,"fork":false,"url":"https://api.github.com/repos/andela/banasko-cfh","forks_url":"https://api.github.com/repos/andela/banasko-cfh/forks","keys_url":"https://api.github.com/repos/andela/banasko-cfh/keys{/key_id}","collaborators_url":"https://api.github.com/repos/andela/banasko-cfh/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/andela/banasko-cfh/teams","hooks_url":"https://api.github.com/repos/andela/banasko-cfh/hooks","issue_events_url":"https://api.github.com/repos/andela/banasko-cfh/issues/events{/number}","events_url":"https://api.github.com/repos/andela/banasko-cfh/events","assignees_url":"https://api.github.com/repos/andela/banasko-cfh/assignees{/user}","branches_url":"https://api.github.com/repos/andela/banasko-cfh/branches{/branch}","tags_url":"https://api.github.com/repos/andela/banasko-cfh/tags","blobs_url":"https://api.github.com/repos/andela/banasko-cfh/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/andela/banasko-cfh/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/andela/banasko-cfh/git/refs{/sha}","trees_url":"https://api.github.com/repos/andela/banasko-cfh/git/trees{/sha}","statuses_url":"https://api.github.com/repos/andela/banasko-cfh/statuses/{sha}","languages_url":"https://api.github.com/repos/andela/banasko-cfh/languages","stargazers_url":"https://api.github.com/repos/andela/banasko-cfh/stargazers","contributors_url":"https://api.github.com/repos/andela/banasko-cfh/contributors","subscribers_url":"https://api.github.com/repos/andela/banasko-cfh/subscribers","subscription_url":"https://api.github.com/repos/andela/banasko-cfh/subscription","commits_url":"https://api.github.com/repos/andela/banasko-cfh/commits{/sha}","git_commits_url":"https://api.github.com/repos/andela/banasko-cfh/git/commits{/sha}","comments_url":"https://api.github.com/repos/andela/banasko-cfh/comments{/number}","issue_comment_url":"https://api.github.com/repos/andela/banasko-cfh/issues/comments{/number}","contents_url":"https://api.github.com/repos/andela/banasko-cfh/contents/{+path}","compare_url":"https://api.github.com/repos/andela/banasko-cfh/compare/{base}...{head}","merges_url":"https://api.github.com/repos/andela/banasko-cfh/merges","archive_url":"https://api.github.com/repos/andela/banasko-cfh/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/andela/banasko-cfh/downloads","issues_url":"https://api.github.com/repos/andela/banasko-cfh/issues{/number}","pulls_url":"https://api.github.com/repos/andela/banasko-cfh/pulls{/number}","milestones_url":"https://api.github.com/repos/andela/banasko-cfh/milestones{/number}","notifications_url":"https://api.github.com/repos/andela/banasko-cfh/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/andela/banasko-cfh/labels{/name}","releases_url":"https://api.github.com/repos/andela/banasko-cfh/releases{/id}","deployments_url":"https://api.github.com/repos/andela/banasko-cfh/deployments","created_at":"2016-11-13T21:42:09Z","updated_at":"2016-12-02T13:39:19Z","pushed_at":"2016-12-11T12:58:29Z","git_url":"git://github.com/andela/banasko-cfh.git","ssh_url":"git@github.com:andela/banasko-cfh.git","clone_url":"https://github.com/andela/banasko-cfh.git","svn_url":"https://github.com/andela/banasko-cfh","homepage":"https://banaskotest.herokuapp.com/#!/","size":10845,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"development"}},"_links":{"self":{"href":"https://api.github.com/repos/andela/banasko-cfh/pulls/13"},"html":{"href":"https://github.com/andela/banasko-cfh/pull/13"},"issue":{"href":"https://api.github.com/repos/andela/banasko-cfh/issues/13"},"comments":{"href":"https://api.github.com/repos/andela/banasko-cfh/issues/13/comments"},"review_comments":{"href":"https://api.github.com/repos/andela/banasko-cfh/pulls/13/comments"},"review_comment":{"href":"https://api.github.com/repos/andela/banasko-cfh/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/andela/banasko-cfh/pulls/13/commits"},"statuses":{"href":"https://api.github.com/repos/andela/banasko-cfh/statuses/e19eb913b6d1fbcebd47c99e2dd4cd38b2d59b82"}}}}
{"actor":{"display_login":"houndci-bot"}}