Full-Stack Web Development with Vue.js and Node

(singke) #1
Building Authentication with passport.js Chapter 6

name: req.body.name,
description: req.body.description,
release_year: req.body.release_year,
genre: req.body.genre
})

movie.save(function (error, movie) {
if (error) { console.log(error); }
res.send(movie)
})
})
}

Replace the contents in controllers/users.js, as follows:


const User = require("../models/User");
const config = require('./../config/Config');
const passport = require('passport');

module.exports.controller = (app) => {
// local strategy
const LocalStrategy = require('passport-local').Strategy;
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function(email, password, done) {
User.getUserByEmail(email, function(err, user){
if (err) { return done(err); }
if (!user) { return done(null, false); }
User.comparePassword(password, user.password, function(err,
isMatch){
if(isMatch) {
return done(null, user);
} else {
return done(null, false);
}
})
});
}
));

app.post('/users/login',
passport.authenticate('local', { failureRedirect: '/users/login' }),
function(req, res) {
res.redirect('/');
});
Free download pdf