Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. var userSchema = new mongoose.Schema({
  2. username: String,
  3. password: String,
  4. datapoint: String,
  5. email: String,
  6. resetPasswordToken: String,
  7. resetPasswordExpires: Date
  8. });
  9.  
  10.  
  11.  
  12. userSchema.pre('save', function(next) {
  13. var user = this;
  14. if (!user.isModified('password')) return next();
  15. bcrypt.genSalt(10, function(err, salt) {
  16. if (err) return next(err);
  17. bcrypt.hash(user.password, salt, null, function(err, hash) {
  18. if (err) return next(err);
  19. user.password = hash;
  20. next();
  21. });
  22. });
  23. });
  24.  
  25. userSchema.methods.comparePassword = function(password) {
  26. return bcrypt.compareSync(password, this.password);
  27. }
  28.  
  29.  
  30. app.post('/login', passport.authenticate('local-login', {
  31. successRedirect: '/',
  32. failureRedirect: '/login',
  33. }));
  34.  
  35. passport.use('local-login', new LocalStrategy({
  36. usernameField: 'username',
  37. passwordField: 'password',
  38. passReqToCallback: true
  39. }, function(req, username, password, done) {
  40. User.findOne({ username: username}, function(err, user) {
  41. if (err) return done(err);
  42.  
  43. if (!user) {
  44. return done(null, false, req.flash('error', 'No user has been found'));
  45. }
  46.  
  47. if (!user.comparePassword(password)) {
  48. return done(null, false, req.flash('error',
  49. 'Wrong password. Please check Caps Lock button, capital letters and retype the password.'));
  50. }
  51. return done(null, user);
  52. });
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement