Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Users with Passport

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.)

like image 963
user1504877 Avatar asked Oct 08 '12 17:10

user1504877


People also ask

Is passport good for authentication?

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.

What is a passport strategy?

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.

Which is better JWT or passport?

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.


1 Answers

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

like image 85
Jared Hanson Avatar answered Sep 29 '22 11:09

Jared Hanson