{"action":"created","comment":{"url":"https://api.github.com/repos/asm-products/confy/pulls/comments/28723792","id":28723792,"diff_hunk":"@@ -0,0 +1,110 @@\n+var crypto = require('crypto');\n+var bcrypt = require('bcrypt');\n+\n+var cryptPass = function (password) {\n+  return bcrypt.hashSync(password, bcrypt.genSaltSync(10));\n+};\n+\n+module.exports = function (app, db) {\n+\n+  //reuqest reset password\n+  app.get('/user/forgot/:email', function (req, res) {\n+    var email = req.params.email\n+      , mail_template = 'reset_password';\n+\n+    //current time in seconds from the start of time.\n+    var currentSeconds = new Date().getTime() / 1000;\n+\n+    // Search for existing user with associated mail\n+    db.view('users', 'email', {keys: [email]}, function (err, body) {\n+      if (err) {\n+        res.status(500);\n+        res.json({ action: 'reset password', code: 'error_occured:'+err.message });\n+        return;\n+      }\n+      if (body.rows.length > 0) {\n+        //take the first object\n+        var firstRow = body.rows[0];\n+        var user = firstRow.value;\n+\n+        if (!user.expire_reset_password || !(user.expire_reset_password > currentSeconds)) {\n+          //doesn't exist create a new token\n+          var token = crypto.randomBytes(20).toString('hex')\n+          user.token_reset_password = token;\n+          user.expire_reset_password = currentSeconds + 7*24*3600;\n+        }\n+        //update the user with the fields we attached\n+        db.insert(user, user._id, function (err, body) {\n+          if (err) {\n+            res.status(500);\n+            res.json({ action: 'reset password', code: 'error_occured:'+err.message });\n+            return;\n+          }\n+\n+          if (!body.ok) {\n+            res.status(500);\n+            res.json({ action: 'reset password', code: 'database_update_failed' });\n+            return;\n+          }\n+        });\n+        //send the mail\n+        app.mail[mail_template](user.email, user, app.errors.capture());","path":"api/users/forgotpass.js","position":51,"original_position":51,"commit_id":"d3b5b5ddb6ec708b08a038d1d394f8c6a22202a5","original_commit_id":"d3b5b5ddb6ec708b08a038d1d394f8c6a22202a5","user":{"login":"pksunkara","id":174703,"avatar_url":"https://avatars.githubusercontent.com/u/174703?v=3","gravatar_id":"","url":"https://api.github.com/users/pksunkara","html_url":"https://github.com/pksunkara","followers_url":"https://api.github.com/users/pksunkara/followers","following_url":"https://api.github.com/users/pksunkara/following{/other_user}","gists_url":"https://api.github.com/users/pksunkara/gists{/gist_id}","starred_url":"https://api.github.com/users/pksunkara/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pksunkara/subscriptions","organizations_url":"https://api.github.com/users/pksunkara/orgs","repos_url":"https://api.github.com/users/pksunkara/repos","events_url":"https://api.github.com/users/pksunkara/events{/privacy}","received_events_url":"https://api.github.com/users/pksunkara/received_events","type":"User","site_admin":false},"body":"This whole 3 lines should be inside the `db.insert`.","created_at":"2015-04-20T20:07:30Z","updated_at":"2015-04-20T20:07:30Z","html_url":"https://github.com/asm-products/confy/pull/8#discussion_r28723792","pull_request_url":"https://api.github.com/repos/asm-products/confy/pulls/8","_links":{"self":{"href":"https://api.github.com/repos/asm-products/confy/pulls/comments/28723792"},"html":{"href":"https://github.com/asm-products/confy/pull/8#discussion_r28723792"},"pull_request":{"href":"https://api.github.com/repos/asm-products/confy/pulls/8"}}},"pull_request":{"url":"https://api.github.com/repos/asm-products/confy/pulls/8","id":33690421,"html_url":"https://github.com/asm-products/confy/pull/8","diff_url":"https://github.com/asm-products/confy/pull/8.diff","patch_url":"https://github.com/asm-products/confy/pull/8.patch","issue_url":"https://api.github.com/repos/asm-products/confy/issues/8","number":8,"state":"open","locked":false,"title":"Implement forgotten password resetting system","user":{"login":"dibosh","id":8603524,"avatar_url":"https://avatars.githubusercontent.com/u/8603524?v=3","gravatar_id":"","url":"https://api.github.com/users/dibosh","html_url":"https://github.com/dibosh","followers_url":"https://api.github.com/users/dibosh/followers","following_url":"https://api.github.com/users/dibosh/following{/other_user}","gists_url":"https://api.github.com/users/dibosh/gists{/gist_id}","starred_url":"https://api.github.com/users/dibosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dibosh/subscriptions","organizations_url":"https://api.github.com/users/dibosh/orgs","repos_url":"https://api.github.com/users/dibosh/repos","events_url":"https://api.github.com/users/dibosh/events{/privacy}","received_events_url":"https://api.github.com/users/dibosh/received_events","type":"User","site_admin":false},"body":"#### Testing the functionality :\r\n\r\n###### How to run- \r\n\r\nRun the service using `sh run_app.sh` from Terminal. The script makes sure the mailgun api key and domain is exported to environment prior to running the service using `node app.js`. This is important because the mailgun api won't be available to the service at runtime otherwise; resulting no email to be sent when the api is fired.\r\n\r\n\r\n`GET` to `user/forgot/` with the format `user/forgot/<email>`\r\nSuccessful response(if the user with that email exists or no error occurs) will look like this - \r\n```\r\n{\r\n\"action\": \"reset password\",\r\n\"code\": \"email_sent\",\r\n\"token\": \"4ba23308bc6090788c7639608969fa0835792b30\"\r\n}\r\n```\r\nexample: `user/forgot/john@doe.com`\r\n\r\nand an email with a link (generated with the token) will be sent to specified email address.\r\n\r\n`POST` to `user/forgot` with the `token` from `GET` call and a password will reset the password for the user associated with the token.\r\n`keys: reset_password_token, password`\r\nExample screenshot when the api is being called using **postman** - \r\n![screen shot 2015-04-21 at 12 08 32 am](https://cloud.githubusercontent.com/assets/8603524/7236919/da946dbe-e7b9-11e4-9a6e-cc170fa7a5d6.png)\r\n\r\nThe password gets encrypted before inserting to database.\r\n","created_at":"2015-04-20T18:04:25Z","updated_at":"2015-04-20T20:07:30Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6daac596ba883c4d6df95d24c078720c90b727fa","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/asm-products/confy/pulls/8/commits","review_comments_url":"https://api.github.com/repos/asm-products/confy/pulls/8/comments","review_comment_url":"https://api.github.com/repos/asm-products/confy/pulls/comments{/number}","comments_url":"https://api.github.com/repos/asm-products/confy/issues/8/comments","statuses_url":"https://api.github.com/repos/asm-products/confy/statuses/d3b5b5ddb6ec708b08a038d1d394f8c6a22202a5","head":{"label":"dibosh:FORGOT-PASSWORD-API","ref":"FORGOT-PASSWORD-API","sha":"d3b5b5ddb6ec708b08a038d1d394f8c6a22202a5","user":{"login":"dibosh","id":8603524,"avatar_url":"https://avatars.githubusercontent.com/u/8603524?v=3","gravatar_id":"","url":"https://api.github.com/users/dibosh","html_url":"https://github.com/dibosh","followers_url":"https://api.github.com/users/dibosh/followers","following_url":"https://api.github.com/users/dibosh/following{/other_user}","gists_url":"https://api.github.com/users/dibosh/gists{/gist_id}","starred_url":"https://api.github.com/users/dibosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dibosh/subscriptions","organizations_url":"https://api.github.com/users/dibosh/orgs","repos_url":"https://api.github.com/users/dibosh/repos","events_url":"https://api.github.com/users/dibosh/events{/privacy}","received_events_url":"https://api.github.com/users/dibosh/received_events","type":"User","site_admin":false},"repo":{"id":34275460,"name":"confy","full_name":"dibosh/confy","owner":{"login":"dibosh","id":8603524,"avatar_url":"https://avatars.githubusercontent.com/u/8603524?v=3","gravatar_id":"","url":"https://api.github.com/users/dibosh","html_url":"https://github.com/dibosh","followers_url":"https://api.github.com/users/dibosh/followers","following_url":"https://api.github.com/users/dibosh/following{/other_user}","gists_url":"https://api.github.com/users/dibosh/gists{/gist_id}","starred_url":"https://api.github.com/users/dibosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dibosh/subscriptions","organizations_url":"https://api.github.com/users/dibosh/orgs","repos_url":"https://api.github.com/users/dibosh/repos","events_url":"https://api.github.com/users/dibosh/events{/privacy}","received_events_url":"https://api.github.com/users/dibosh/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dibosh/confy","description":"Helping developers seperate config from code","fork":true,"url":"https://api.github.com/repos/dibosh/confy","forks_url":"https://api.github.com/repos/dibosh/confy/forks","keys_url":"https://api.github.com/repos/dibosh/confy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dibosh/confy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dibosh/confy/teams","hooks_url":"https://api.github.com/repos/dibosh/confy/hooks","issue_events_url":"https://api.github.com/repos/dibosh/confy/issues/events{/number}","events_url":"https://api.github.com/repos/dibosh/confy/events","assignees_url":"https://api.github.com/repos/dibosh/confy/assignees{/user}","branches_url":"https://api.github.com/repos/dibosh/confy/branches{/branch}","tags_url":"https://api.github.com/repos/dibosh/confy/tags","blobs_url":"https://api.github.com/repos/dibosh/confy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dibosh/confy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dibosh/confy/git/refs{/sha}","trees_url":"https://api.github.com/repos/dibosh/confy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dibosh/confy/statuses/{sha}","languages_url":"https://api.github.com/repos/dibosh/confy/languages","stargazers_url":"https://api.github.com/repos/dibosh/confy/stargazers","contributors_url":"https://api.github.com/repos/dibosh/confy/contributors","subscribers_url":"https://api.github.com/repos/dibosh/confy/subscribers","subscription_url":"https://api.github.com/repos/dibosh/confy/subscription","commits_url":"https://api.github.com/repos/dibosh/confy/commits{/sha}","git_commits_url":"https://api.github.com/repos/dibosh/confy/git/commits{/sha}","comments_url":"https://api.github.com/repos/dibosh/confy/comments{/number}","issue_comment_url":"https://api.github.com/repos/dibosh/confy/issues/comments{/number}","contents_url":"https://api.github.com/repos/dibosh/confy/contents/{+path}","compare_url":"https://api.github.com/repos/dibosh/confy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dibosh/confy/merges","archive_url":"https://api.github.com/repos/dibosh/confy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dibosh/confy/downloads","issues_url":"https://api.github.com/repos/dibosh/confy/issues{/number}","pulls_url":"https://api.github.com/repos/dibosh/confy/pulls{/number}","milestones_url":"https://api.github.com/repos/dibosh/confy/milestones{/number}","notifications_url":"https://api.github.com/repos/dibosh/confy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dibosh/confy/labels{/name}","releases_url":"https://api.github.com/repos/dibosh/confy/releases{/id}","created_at":"2015-04-20T17:31:07Z","updated_at":"2015-04-20T17:31:08Z","pushed_at":"2015-04-20T18:08:37Z","git_url":"git://github.com/dibosh/confy.git","ssh_url":"git@github.com:dibosh/confy.git","clone_url":"https://github.com/dibosh/confy.git","svn_url":"https://github.com/dibosh/confy","homepage":"https://assemblymade.com/confy","size":344,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"asm-products:master","ref":"master","sha":"791a866881e55f38de173b541500550a4f645dcc","user":{"login":"asm-products","id":6548361,"avatar_url":"https://avatars.githubusercontent.com/u/6548361?v=3","gravatar_id":"","url":"https://api.github.com/users/asm-products","html_url":"https://github.com/asm-products","followers_url":"https://api.github.com/users/asm-products/followers","following_url":"https://api.github.com/users/asm-products/following{/other_user}","gists_url":"https://api.github.com/users/asm-products/gists{/gist_id}","starred_url":"https://api.github.com/users/asm-products/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asm-products/subscriptions","organizations_url":"https://api.github.com/users/asm-products/orgs","repos_url":"https://api.github.com/users/asm-products/repos","events_url":"https://api.github.com/users/asm-products/events{/privacy}","received_events_url":"https://api.github.com/users/asm-products/received_events","type":"Organization","site_admin":false},"repo":{"id":18547725,"name":"confy","full_name":"asm-products/confy","owner":{"login":"asm-products","id":6548361,"avatar_url":"https://avatars.githubusercontent.com/u/6548361?v=3","gravatar_id":"","url":"https://api.github.com/users/asm-products","html_url":"https://github.com/asm-products","followers_url":"https://api.github.com/users/asm-products/followers","following_url":"https://api.github.com/users/asm-products/following{/other_user}","gists_url":"https://api.github.com/users/asm-products/gists{/gist_id}","starred_url":"https://api.github.com/users/asm-products/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asm-products/subscriptions","organizations_url":"https://api.github.com/users/asm-products/orgs","repos_url":"https://api.github.com/users/asm-products/repos","events_url":"https://api.github.com/users/asm-products/events{/privacy}","received_events_url":"https://api.github.com/users/asm-products/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/asm-products/confy","description":"Helping developers seperate config from code","fork":false,"url":"https://api.github.com/repos/asm-products/confy","forks_url":"https://api.github.com/repos/asm-products/confy/forks","keys_url":"https://api.github.com/repos/asm-products/confy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/asm-products/confy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/asm-products/confy/teams","hooks_url":"https://api.github.com/repos/asm-products/confy/hooks","issue_events_url":"https://api.github.com/repos/asm-products/confy/issues/events{/number}","events_url":"https://api.github.com/repos/asm-products/confy/events","assignees_url":"https://api.github.com/repos/asm-products/confy/assignees{/user}","branches_url":"https://api.github.com/repos/asm-products/confy/branches{/branch}","tags_url":"https://api.github.com/repos/asm-products/confy/tags","blobs_url":"https://api.github.com/repos/asm-products/confy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/asm-products/confy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/asm-products/confy/git/refs{/sha}","trees_url":"https://api.github.com/repos/asm-products/confy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/asm-products/confy/statuses/{sha}","languages_url":"https://api.github.com/repos/asm-products/confy/languages","stargazers_url":"https://api.github.com/repos/asm-products/confy/stargazers","contributors_url":"https://api.github.com/repos/asm-products/confy/contributors","subscribers_url":"https://api.github.com/repos/asm-products/confy/subscribers","subscription_url":"https://api.github.com/repos/asm-products/confy/subscription","commits_url":"https://api.github.com/repos/asm-products/confy/commits{/sha}","git_commits_url":"https://api.github.com/repos/asm-products/confy/git/commits{/sha}","comments_url":"https://api.github.com/repos/asm-products/confy/comments{/number}","issue_comment_url":"https://api.github.com/repos/asm-products/confy/issues/comments{/number}","contents_url":"https://api.github.com/repos/asm-products/confy/contents/{+path}","compare_url":"https://api.github.com/repos/asm-products/confy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/asm-products/confy/merges","archive_url":"https://api.github.com/repos/asm-products/confy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/asm-products/confy/downloads","issues_url":"https://api.github.com/repos/asm-products/confy/issues{/number}","pulls_url":"https://api.github.com/repos/asm-products/confy/pulls{/number}","milestones_url":"https://api.github.com/repos/asm-products/confy/milestones{/number}","notifications_url":"https://api.github.com/repos/asm-products/confy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/asm-products/confy/labels{/name}","releases_url":"https://api.github.com/repos/asm-products/confy/releases{/id}","created_at":"2014-04-08T06:27:28Z","updated_at":"2015-03-31T17:33:00Z","pushed_at":"2015-04-20T18:08:37Z","git_url":"git://github.com/asm-products/confy.git","ssh_url":"git@github.com:asm-products/confy.git","clone_url":"https://github.com/asm-products/confy.git","svn_url":"https://github.com/asm-products/confy","homepage":"https://assemblymade.com/confy","size":893,"stargazers_count":7,"watchers_count":7,"language":"JavaScript","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":1,"forks":5,"open_issues":1,"watchers":7,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/asm-products/confy/pulls/8"},"html":{"href":"https://github.com/asm-products/confy/pull/8"},"issue":{"href":"https://api.github.com/repos/asm-products/confy/issues/8"},"comments":{"href":"https://api.github.com/repos/asm-products/confy/issues/8/comments"},"review_comments":{"href":"https://api.github.com/repos/asm-products/confy/pulls/8/comments"},"review_comment":{"href":"https://api.github.com/repos/asm-products/confy/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/asm-products/confy/pulls/8/commits"},"statuses":{"href":"https://api.github.com/repos/asm-products/confy/statuses/d3b5b5ddb6ec708b08a038d1d394f8c6a22202a5"}}}}