{"action":"created","comment":{"url":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/comments/30188265","id":30188265,"diff_hunk":"@@ -0,0 +1,133 @@\n+var Db        = require('../config/database');\n+var mongoose  = require('mongoose');\n+var crypto    = require('crypto')\n+\n+// Generates a random string to use as a session token\n+function generateToken() {\n+  return crypto.randomBytes(256).toString('base64');\n+}\n+\n+// Create the schema for a session\n+var SessionSchema = mongoose.Schema({\n+\n+  // Defaults to a string of 256 characters\n+  token     : { type : String, default : generateToken },\n+\n+  // Defaults to the current time and expires in 7 days\n+  createdAt : { type : Date, default : Date.now, expires : '7d' },\n+\n+  // Holds the ObjectId of the user logged into this session\n+  userId    : { type : mongoose.Schema.Types.ObjectId, required : true}\n+});\n+\n+// Creates a collection named sessions in MongoDB\n+var SessionMongoModel = Db.model('sessions', SessionSchema);\n+\n+// Takes a userId as an argument and returns the token string to the callback\n+function findSessionByUserId(userId, callback) {\n+  SessionMongoModel.findOne({ userId : userId.toLowerCase() }, function(err, session) {\n+    if (err) {\n+      // TODO: Error message?\n+      callback(err, null);\n+    } else if (!session) {\n+      callback({\n+        code    : 400,\n+        message : 'The session does not exist.'\n+      });\n+    } else {\n+      callback(null, session.token);\n+    }\n+  });\n+}\n+\n+// Takes a token string as an argument and returns the userId to the callback\n+function findUserByToken(clientToken, callback) {\n+  SessionMongoModel.findOne({ token : clientToken }, function(err, session) {\n+    if (err) {\n+      // TODO: Error message?\n+      callback(err, null);\n+    } else if (!session) {\n+      callback({\n+        code    : 400,\n+        message : 'The session does not exists.'\n+      });\n+    } else {\n+      callback(null, session.userId);\n+    }\n+  });\n+}\n+\n+// Creates a new session and attach user ObjectId to it\n+function createSession(userId, callback) {\n+  var newSession = new SessionMongoModel({ userId : userId });\n+\n+  newSession.save(function (err, session) {\n+    if (err) {\n+      // TODO: Error message?\n+      callback(err, null);\n+    } else if (!session) {\n+      callback({\n+        code    : 400,\n+        message : 'Session creation failed.'\n+      });\n+    } else {\n+      callback(null, session);\n+    }\n+  });\n+}\n+\n+// Takes a token string and deletes the session with that string from MongoDB\n+function destroySession(clientToken, callback) {\n+  SessionMongoModel.findOneAndRemove({ token : clientToken }, \n+   function(err) {\n+    if (err) {\n+      // TODO: Error message?\n+      callback(err);\n+      return; \n+    }\n+    callback(null);\n+  });\n+}\n+\n+// Takes a token string, deletes the associated session, and makes a new one,\n+// returning the new token to the callback\n+function refreshSession(clientToken, callback) {\n+  SessionMongoModel.findOne({ token : clientToken },\n+                            function(err, session) {\n+    if (err) {\n+      // TODO: Error message?\n+      callback(err);\n+    } else if (!session) {\n+      callback({\n+        code    : 400,\n+        message : 'The session does not exists.'\n+      });\n+    } else {\n+      createSession(session.userId, function(err, newSession) {\n+        if (err) {\n+          callback(err);\n+        } else {\n+          destroySession(clientToken, function(err) {","path":"models/sessions.js","position":110,"original_position":110,"commit_id":"ba527cf01d7cc5689c02f3e5e1336f7b2a0b809c","original_commit_id":"ba527cf01d7cc5689c02f3e5e1336f7b2a0b809c","user":{"login":"chrisngobanh","id":8410793,"avatar_url":"https://avatars.githubusercontent.com/u/8410793?v=3","gravatar_id":"","url":"https://api.github.com/users/chrisngobanh","html_url":"https://github.com/chrisngobanh","followers_url":"https://api.github.com/users/chrisngobanh/followers","following_url":"https://api.github.com/users/chrisngobanh/following{/other_user}","gists_url":"https://api.github.com/users/chrisngobanh/gists{/gist_id}","starred_url":"https://api.github.com/users/chrisngobanh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrisngobanh/subscriptions","organizations_url":"https://api.github.com/users/chrisngobanh/orgs","repos_url":"https://api.github.com/users/chrisngobanh/repos","events_url":"https://api.github.com/users/chrisngobanh/events{/privacy}","received_events_url":"https://api.github.com/users/chrisngobanh/received_events","type":"User","site_admin":false},"body":"Use async to avoid callback hell","created_at":"2015-05-12T22:41:05Z","updated_at":"2015-05-12T22:41:05Z","html_url":"https://github.com/DevelopersGuild/daze-backend/pull/2#discussion_r30188265","pull_request_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/comments/30188265"},"html":{"href":"https://github.com/DevelopersGuild/daze-backend/pull/2#discussion_r30188265"},"pull_request":{"href":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/2","id":35099850,"html_url":"https://github.com/DevelopersGuild/daze-backend/pull/2","diff_url":"https://github.com/DevelopersGuild/daze-backend/pull/2.diff","patch_url":"https://github.com/DevelopersGuild/daze-backend/pull/2.patch","issue_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues/2","number":2,"state":"open","locked":false,"title":"Models and Routes","user":{"login":"tuan91187dsf","id":11918574,"avatar_url":"https://avatars.githubusercontent.com/u/11918574?v=3","gravatar_id":"","url":"https://api.github.com/users/tuan91187dsf","html_url":"https://github.com/tuan91187dsf","followers_url":"https://api.github.com/users/tuan91187dsf/followers","following_url":"https://api.github.com/users/tuan91187dsf/following{/other_user}","gists_url":"https://api.github.com/users/tuan91187dsf/gists{/gist_id}","starred_url":"https://api.github.com/users/tuan91187dsf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tuan91187dsf/subscriptions","organizations_url":"https://api.github.com/users/tuan91187dsf/orgs","repos_url":"https://api.github.com/users/tuan91187dsf/repos","events_url":"https://api.github.com/users/tuan91187dsf/events{/privacy}","received_events_url":"https://api.github.com/users/tuan91187dsf/received_events","type":"User","site_admin":false},"body":"Has been tested for successful requests for all requests on google docs. Some errors will still send a success message but will not perform any actions on the database.","created_at":"2015-05-11T06:04:20Z","updated_at":"2015-05-12T22:41:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d6a96e16216f22ca456bbc83902615f95473e6b1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/2/commits","review_comments_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/2/comments","review_comment_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/comments{/number}","comments_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues/2/comments","statuses_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/statuses/ba527cf01d7cc5689c02f3e5e1336f7b2a0b809c","head":{"label":"DevelopersGuild:modelsAndRoutes","ref":"modelsAndRoutes","sha":"ba527cf01d7cc5689c02f3e5e1336f7b2a0b809c","user":{"login":"DevelopersGuild","id":5997729,"avatar_url":"https://avatars.githubusercontent.com/u/5997729?v=3","gravatar_id":"","url":"https://api.github.com/users/DevelopersGuild","html_url":"https://github.com/DevelopersGuild","followers_url":"https://api.github.com/users/DevelopersGuild/followers","following_url":"https://api.github.com/users/DevelopersGuild/following{/other_user}","gists_url":"https://api.github.com/users/DevelopersGuild/gists{/gist_id}","starred_url":"https://api.github.com/users/DevelopersGuild/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DevelopersGuild/subscriptions","organizations_url":"https://api.github.com/users/DevelopersGuild/orgs","repos_url":"https://api.github.com/users/DevelopersGuild/repos","events_url":"https://api.github.com/users/DevelopersGuild/events{/privacy}","received_events_url":"https://api.github.com/users/DevelopersGuild/received_events","type":"Organization","site_admin":false},"repo":{"id":33753505,"name":"daze-backend","full_name":"DevelopersGuild/daze-backend","owner":{"login":"DevelopersGuild","id":5997729,"avatar_url":"https://avatars.githubusercontent.com/u/5997729?v=3","gravatar_id":"","url":"https://api.github.com/users/DevelopersGuild","html_url":"https://github.com/DevelopersGuild","followers_url":"https://api.github.com/users/DevelopersGuild/followers","following_url":"https://api.github.com/users/DevelopersGuild/following{/other_user}","gists_url":"https://api.github.com/users/DevelopersGuild/gists{/gist_id}","starred_url":"https://api.github.com/users/DevelopersGuild/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DevelopersGuild/subscriptions","organizations_url":"https://api.github.com/users/DevelopersGuild/orgs","repos_url":"https://api.github.com/users/DevelopersGuild/repos","events_url":"https://api.github.com/users/DevelopersGuild/events{/privacy}","received_events_url":"https://api.github.com/users/DevelopersGuild/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/DevelopersGuild/daze-backend","description":"[Spring 2015] This is Waze, but for De Anza. (Backend)","fork":false,"url":"https://api.github.com/repos/DevelopersGuild/daze-backend","forks_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/forks","keys_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/keys{/key_id}","collaborators_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/teams","hooks_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/hooks","issue_events_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues/events{/number}","events_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/events","assignees_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/assignees{/user}","branches_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/branches{/branch}","tags_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/tags","blobs_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/refs{/sha}","trees_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/trees{/sha}","statuses_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/statuses/{sha}","languages_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/languages","stargazers_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/stargazers","contributors_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/contributors","subscribers_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/subscribers","subscription_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/subscription","commits_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/commits{/sha}","git_commits_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/commits{/sha}","comments_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/comments{/number}","issue_comment_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues/comments{/number}","contents_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/contents/{+path}","compare_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/compare/{base}...{head}","merges_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/merges","archive_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/downloads","issues_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues{/number}","pulls_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls{/number}","milestones_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/milestones{/number}","notifications_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/labels{/name}","releases_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/releases{/id}","created_at":"2015-04-10T22:26:25Z","updated_at":"2015-04-28T22:36:08Z","pushed_at":"2015-05-12T17:14:49Z","git_url":"git://github.com/DevelopersGuild/daze-backend.git","ssh_url":"git@github.com:DevelopersGuild/daze-backend.git","clone_url":"https://github.com/DevelopersGuild/daze-backend.git","svn_url":"https://github.com/DevelopersGuild/daze-backend","homepage":"","size":212,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"master"}},"base":{"label":"DevelopersGuild:master","ref":"master","sha":"31dc0b0f08f46be915b74cd9c84330f828a4a42d","user":{"login":"DevelopersGuild","id":5997729,"avatar_url":"https://avatars.githubusercontent.com/u/5997729?v=3","gravatar_id":"","url":"https://api.github.com/users/DevelopersGuild","html_url":"https://github.com/DevelopersGuild","followers_url":"https://api.github.com/users/DevelopersGuild/followers","following_url":"https://api.github.com/users/DevelopersGuild/following{/other_user}","gists_url":"https://api.github.com/users/DevelopersGuild/gists{/gist_id}","starred_url":"https://api.github.com/users/DevelopersGuild/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DevelopersGuild/subscriptions","organizations_url":"https://api.github.com/users/DevelopersGuild/orgs","repos_url":"https://api.github.com/users/DevelopersGuild/repos","events_url":"https://api.github.com/users/DevelopersGuild/events{/privacy}","received_events_url":"https://api.github.com/users/DevelopersGuild/received_events","type":"Organization","site_admin":false},"repo":{"id":33753505,"name":"daze-backend","full_name":"DevelopersGuild/daze-backend","owner":{"login":"DevelopersGuild","id":5997729,"avatar_url":"https://avatars.githubusercontent.com/u/5997729?v=3","gravatar_id":"","url":"https://api.github.com/users/DevelopersGuild","html_url":"https://github.com/DevelopersGuild","followers_url":"https://api.github.com/users/DevelopersGuild/followers","following_url":"https://api.github.com/users/DevelopersGuild/following{/other_user}","gists_url":"https://api.github.com/users/DevelopersGuild/gists{/gist_id}","starred_url":"https://api.github.com/users/DevelopersGuild/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DevelopersGuild/subscriptions","organizations_url":"https://api.github.com/users/DevelopersGuild/orgs","repos_url":"https://api.github.com/users/DevelopersGuild/repos","events_url":"https://api.github.com/users/DevelopersGuild/events{/privacy}","received_events_url":"https://api.github.com/users/DevelopersGuild/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/DevelopersGuild/daze-backend","description":"[Spring 2015] This is Waze, but for De Anza. (Backend)","fork":false,"url":"https://api.github.com/repos/DevelopersGuild/daze-backend","forks_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/forks","keys_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/keys{/key_id}","collaborators_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/teams","hooks_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/hooks","issue_events_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues/events{/number}","events_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/events","assignees_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/assignees{/user}","branches_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/branches{/branch}","tags_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/tags","blobs_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/refs{/sha}","trees_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/trees{/sha}","statuses_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/statuses/{sha}","languages_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/languages","stargazers_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/stargazers","contributors_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/contributors","subscribers_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/subscribers","subscription_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/subscription","commits_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/commits{/sha}","git_commits_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/git/commits{/sha}","comments_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/comments{/number}","issue_comment_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues/comments{/number}","contents_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/contents/{+path}","compare_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/compare/{base}...{head}","merges_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/merges","archive_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/downloads","issues_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues{/number}","pulls_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls{/number}","milestones_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/milestones{/number}","notifications_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/labels{/name}","releases_url":"https://api.github.com/repos/DevelopersGuild/daze-backend/releases{/id}","created_at":"2015-04-10T22:26:25Z","updated_at":"2015-04-28T22:36:08Z","pushed_at":"2015-05-12T17:14:49Z","git_url":"git://github.com/DevelopersGuild/daze-backend.git","ssh_url":"git@github.com:DevelopersGuild/daze-backend.git","clone_url":"https://github.com/DevelopersGuild/daze-backend.git","svn_url":"https://github.com/DevelopersGuild/daze-backend","homepage":"","size":212,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/2"},"html":{"href":"https://github.com/DevelopersGuild/daze-backend/pull/2"},"issue":{"href":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues/2"},"comments":{"href":"https://api.github.com/repos/DevelopersGuild/daze-backend/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/DevelopersGuild/daze-backend/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/DevelopersGuild/daze-backend/statuses/ba527cf01d7cc5689c02f3e5e1336f7b2a0b809c"}}}}