Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var userSchema = new mongoose.Schema({
- username: String,
- password: String,
- datapoint: String,
- email: String,
- resetPasswordToken: String,
- resetPasswordExpires: Date
- });
- userSchema.pre('save', function(next) {
- var user = this;
- if (!user.isModified('password')) return next();
- bcrypt.genSalt(10, function(err, salt) {
- if (err) return next(err);
- bcrypt.hash(user.password, salt, null, function(err, hash) {
- if (err) return next(err);
- user.password = hash;
- next();
- });
- });
- });
- userSchema.methods.comparePassword = function(password) {
- return bcrypt.compareSync(password, this.password);
- }
- app.post('/login', passport.authenticate('local-login', {
- successRedirect: '/',
- failureRedirect: '/login',
- }));
- passport.use('local-login', new LocalStrategy({
- usernameField: 'username',
- passwordField: 'password',
- passReqToCallback: true
- }, function(req, username, password, done) {
- User.findOne({ username: username}, function(err, user) {
- if (err) return done(err);
- if (!user) {
- return done(null, false, req.flash('error', 'No user has been found'));
- }
- if (!user.comparePassword(password)) {
- return done(null, false, req.flash('error',
- 'Wrong password. Please check Caps Lock button, capital letters and retype the password.'));
- }
- return done(null, user);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement