Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding additional fields to passport.js local strategy

By default, passport.js only accepts username and password in its middleware.

http://passportjs.org/guide/username-password/

How do I add a third field? In my case, I need username, email, and password.

like image 284
metalaureate Avatar asked Sep 18 '14 02:09

metalaureate


People also ask

Can passport use multiple strategies?

Passport's middleware is built in a way that allows you to use multiple strategies in one passport.

How do I create a custom passport strategy?

Configure Strategy The custom authentication strategy authenticates users by custom logic of your choosing. The strategy requires a verify callback, which is where the custom logic goes and calls done providing a user. Note that, req is always passed as the first parameter to the verify callback.

What are strategies in passport js?

Strategies are responsible for authenticating requests, which they accomplish by implementing an authentication mechanism. Authentication mechanisms define how to encode a credential, such as a password or an assertion from an identity provider (IdP), in a request.

What is serialization in passport js?

Passport uses serializeUser function to persist user data (after successful authentication) into session. Function deserializeUser is used to retrieve user data from session.


1 Answers

Very simple, you just need to add req parameter and set passReqToCallback to true

passport.use('loginpassport', new LocalStrategy({
   usernameField: 'uname',
   passwordField: 'upass',
   passReqToCallback: true
   }, function (req, username, password, done) {
        var something = req.body.xxxxx;
   }));
like image 54
Binh Avatar answered Oct 18 '22 11:10

Binh