Guest User

Untitled

a guest
Mar 10th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. var mongoose = require(mongoose),
  2. User = require(./user-model);
  3.  
  4. var connStr = mongodb://localhost:27017/mongoose-bcrypt-test;
  5. mongoose.connect(connStr, function(err) {
  6. if (err) throw err;
  7. console.log(Successfully connected to MongoDB);
  8. });
  9.  
  10. // create a user a new user
  11. var testUser = new User({
  12. username: jmar777,
  13. password: Password;
  14. });
  15.  
  16. // save user to database
  17. testUser.save(function(err) {
  18. if (err) throw err;
  19.  
  20. // fetch user and test password verification
  21. User.findOne({ username: 'jmar777' }, function(err, user) {
  22. if (err) throw err;
  23.  
  24. // test a matching password
  25. user.comparePassword('Password', function(err, isMatch) {
  26. if (err) throw err;
  27. console.log('Password:', isMatch); // -> Password: true
  28. });
  29.  
  30. // test a failing password
  31. user.comparePassword('123Password', function(err, isMatch) {
  32. if (err) throw err;
  33. console.log('123Password:', isMatch); // -> 123Password: false
  34. });
  35. });
Add Comment
Please, Sign In to add comment