Answer the question
In order to leave comments, you need to log in
How to connect correctly Passport.js?
I take an example from off. site.
I paste the code into the app.js file.
app.use(express.urlencoded({extended: true}));
var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function(err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}
));
app.post('/login',
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login',
failureFlash: true })
);
app.use(passport.initialize());
app.use(passport.session());
<form action="/login" method="post">
<div>
<label>Username:</label>
<input type="text" name="username"/>
</div>
<div>
<label>Password:</label>
<input type="password" name="password"/>
</div>
<div>
<input type="submit" value="Log In"/>
</div>
</form>
Answer the question
In order to leave comments, you need to log in
is passport connected to the app?
app.use(passport.initialize());
app.use(passport.session());
app.use(passport.initialize());
app.use(passport.session());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question