{"action":"created","issue":{"url":"https://api.github.com/repos/dcodeIO/bcrypt.js/issues/45","repository_url":"https://api.github.com/repos/dcodeIO/bcrypt.js","labels_url":"https://api.github.com/repos/dcodeIO/bcrypt.js/issues/45/labels{/name}","comments_url":"https://api.github.com/repos/dcodeIO/bcrypt.js/issues/45/comments","events_url":"https://api.github.com/repos/dcodeIO/bcrypt.js/issues/45/events","html_url":"https://github.com/dcodeIO/bcrypt.js/issues/45","id":183298446,"number":45,"title":"Unable to compare password","user":{"login":"wsfuller","id":3144419,"avatar_url":"https://avatars.githubusercontent.com/u/3144419?v=3","gravatar_id":"","url":"https://api.github.com/users/wsfuller","html_url":"https://github.com/wsfuller","followers_url":"https://api.github.com/users/wsfuller/followers","following_url":"https://api.github.com/users/wsfuller/following{/other_user}","gists_url":"https://api.github.com/users/wsfuller/gists{/gist_id}","starred_url":"https://api.github.com/users/wsfuller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wsfuller/subscriptions","organizations_url":"https://api.github.com/users/wsfuller/orgs","repos_url":"https://api.github.com/users/wsfuller/repos","events_url":"https://api.github.com/users/wsfuller/events{/privacy}","received_events_url":"https://api.github.com/users/wsfuller/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-10-16T23:03:34Z","updated_at":"2016-11-20T12:23:54Z","closed_at":null,"body":"So I've looked over the documentation for a couple of hours and been looking over examples and tutorials for quite sometime and nothing seems to work. All I want to do is salt and hash a users password and store it. Then have the user login, compare the passwords, and then send a JSON Web Token. \n\nTest Credentials:\nemail: test@test.com\npassword: password\n\n**user.routes.js**\n\n```\nvar express     = require('express');\nvar router      = express.Router();\nvar jwt         = require('jsonwebtoken');\nvar bcrypt      = require('bcryptjs');\nvar config      = require('../config/database');\n\n// POST create user\n...\nbcrypt.genSalt(10, function(err, salt) {\n    bcrypt.hash(\"superSecret\", salt, function(err, hash) {\n      user.password = hash;\n      user.save();\n      res.json({success: true, message: 'Create user successful'});\n    });\n  });\n...\n\n// POST login\n...\nbcrypt.compare(req.body.password, \"superSecret\", function(err, res) {\n    if(user.password != req.body.password){\n      console.log('password incorrect');\n      //res.send('password incorrect');\n    } else {\n      console.log('password is correct send JWT');\n      //res.send('password correct send JWT');\n    }\n  });\n...\n```\n\nI'm not quite sure what actually is supposed to be passed in the `compare` method. The documentation has `bcrypt.compare(\"B4c0/\\/\", hash, function(err, res){..})` but hash is undefined. \n\nSo should I be declaring `var hash = ...` initially and if I am doesn't that defeat the purpose of `bcrypt.hash(\"superSecret\", salt, function(err, hash){...}); from the Create user? \n"},"comment":{"url":"https://api.github.com/repos/dcodeIO/bcrypt.js/issues/comments/261775085","html_url":"https://github.com/dcodeIO/bcrypt.js/issues/45#issuecomment-261775085","issue_url":"https://api.github.com/repos/dcodeIO/bcrypt.js/issues/45","id":261775085,"user":{"login":"ribbibisaria","id":13076535,"avatar_url":"https://avatars.githubusercontent.com/u/13076535?v=3","gravatar_id":"","url":"https://api.github.com/users/ribbibisaria","html_url":"https://github.com/ribbibisaria","followers_url":"https://api.github.com/users/ribbibisaria/followers","following_url":"https://api.github.com/users/ribbibisaria/following{/other_user}","gists_url":"https://api.github.com/users/ribbibisaria/gists{/gist_id}","starred_url":"https://api.github.com/users/ribbibisaria/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ribbibisaria/subscriptions","organizations_url":"https://api.github.com/users/ribbibisaria/orgs","repos_url":"https://api.github.com/users/ribbibisaria/repos","events_url":"https://api.github.com/users/ribbibisaria/events{/privacy}","received_events_url":"https://api.github.com/users/ribbibisaria/received_events","type":"User","site_admin":false},"created_at":"2016-11-20T12:23:54Z","updated_at":"2016-11-20T12:23:54Z","body":"Taking your example and making changes accordingly for correct results\r\nvar express     = require('express');\r\nvar router      = express.Router();\r\nvar jwt         = require('jsonwebtoken');\r\nvar bcrypt      = require('bcryptjs');\r\nvar config      = require('../config/database');\r\n\r\n// POST create user\r\n...\r\nbcrypt.genSalt(10, function(err, salt) {\r\n\r\n//You need to remember 'superSecret' is supposed to be the password taken from user\r\n// for example req.body.password instead of 'superSecret' . This is just to clarify\r\n bcrypt.hash(\"superSecret\", salt, function(err, hash) {\r\n    user.password = hash;\r\n      user.save();\r\n      res.json({success: true, message: 'Create user successful'});\r\n    });\r\n  });\r\n...\r\n\r\n// POST login\r\n...\r\n//for compare just fetch the password as you saved it from previous createUser\r\n//Basically find and fetch the user you saved\r\n//in place of \"superSecret\" use the hashed password you have fetched AS IS\r\n//Eg. bcrypt.compare(req.body.password, userFound.hashedPassword, function(err, response) { \r\n//this will return response=true or response=false accordingly after comparing\r\n//Id suggest changing  'res' to 'response' since we use term res for our own response  \r\n\r\nbcrypt.compare(req.body.password, \"superSecret\", function(err, res) {\r\n    if(user.password != req.body.password){\r\n      console.log('password incorrect');\r\n      //res.send('password incorrect');\r\n    } else {\r\n      console.log('password is correct send JWT');\r\n      //res.send('password correct send JWT');\r\n    }\r\n  });\r\n..."}}
{"actor":{"display_login":"ribbibisaria"}}