I'm using Node.js and intend to use Passport for authentication. However, all of the examples I see online assume the user has already been created. I'd like to know how to create a user's session correctly, after they are saved in the database (I have this part working), so that Passport will recognize them. (I do not want to save the new user and then force them to go to a login page.)
Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.
Passport's local strategy is a Node. js module that allows you to implement a username/password authentication mechanism. You'll need to install it like any other module and configure it to use your User Mongoose model.
The main difference between Passport and Passport-JWT is that Passport does not have any particular method for authentication instead many methods are implemented using passport as strategies for authentication whereas Passport-JWT is a strategy which uses web token method using passport for authentication.
Just call:
// user is the user instance you just registered and created
req.logIn(user, function(err) {
if (err) return next(err);
// login success!
res.redirect('/home'); // or whereever
});
Documentation for this function is in the code (I need to add it to the guide): https://github.com/jaredhanson/passport/blob/master/lib/http/request.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With