Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It possible to use Passport.js without Expressjs?

I have an app which uses nodejs but I don't serve my pages via ExpressJS. It is a simple http nodejs app which I will migrate to nodejs at some stage.

However, I am playing around with Passportjs and currently I am getting 'passport.intitalize() middleware not in use' errors which from the documentation mention 'connect' and 'Express' usage.

Can I use passportjs without Expressjs?

like image 362
JD. Avatar asked Apr 29 '14 22:04

JD.


2 Answers

Passport Strategies expect inputs on certain channels (e.g. 'Local' looks for username and password fields on <OBJ>.body), but if you provide for these Strategy requirements directly you can use Passport just fine without Express/Connect.

Take a look directly at comments lifted from the relevant Passport code:

https://github.com/jaredhanson/passport/blob/master/lib/authenticator.js#L146

passport.authenticate('local', function(err, user) {
  if (!user) { return res.redirect('/login'); }
  res.end('Authenticated!');
})(req, res);

If you provide req and res as your own objects (paying attention to the requirements of the Strategy you're using and whatever you're using of req/res in authenticate), you have total control.

(Or just write a simple custom framework wrapper).

like image 122
papercowboy Avatar answered Oct 03 '22 03:10

papercowboy


Short version: No, express is required for regular passport usage.

Long version: You could technically use a large portion of passport's code in a non-express application, but that could create all sorts of edge cases you don't want in your auth code. It'd also probably be easier just to convert your application to express.

like image 45
SomeKittens Avatar answered Oct 03 '22 05:10

SomeKittens