passport-local-mongoose icon indicating copy to clipboard operation
passport-local-mongoose copied to clipboard

usernameField settings outlined in the docs do not work

Open naossoan opened this issue 2 years ago • 4 comments

Other than doing this in the user schema: userSchema.plugin(passportLocalMongoose, { usernameField: 'email' }); (or whatever field you want to use)

In the docs for passport-local-mongoose it says to replace this:

passport.use(new LocalStrategy(User.authenticate())); with this:

// CHANGE: USE "createStrategy" INSTEAD OF "authenticate"
passport.use(User.createStrategy());

if you want to use a different field for username.

It says directly below this:

The reason for this functionality is that when using the usernameField option to specify an alternative usernameField name, for example "email" passport-local would still expect your frontend login form to contain an input field with name "username" instead of email. This can be configured for passport-local but this is double the work. So we got this shortcut implemented.

However, this does not work at all.

Upon user creation, the first user would work fine, however, subsequent attempts would come back with an error about a duplicate field "username" of "null."

I may have been doing something wrong, but I was following the docs as best as I could. In order to get this to work for me, I had to do this:

passport.use(new LocalStrategy({
    usernameField: 'email'
}, User.authenticate()));

NOT passport.use(User.createStrategy()); OR passport.use(new LocalStrategy(User.authenticate()));

I also tried this, as it says to replace User.authenticate() with User.createStrategy(). So I took that literally and tried this but also does not work (crashes app because LocalStrategy needs a callback function) passport.use(new LocalStrategy(User.createStrategy()));

naossoan avatar Aug 03 '22 07:08 naossoan

For some reason, even

passport.use(new LocalStrategy({
    usernameField: 'email'
}, User.authenticate()));

doesn't work for me (getting duplicate username field error). Tried passport.use(User.createStrategy()); as well, no luck.

Anyone running into similar issues?

delivey avatar Nov 07 '22 14:11 delivey

Up on this issue ? I seem to got it too and I don't see any fixes version : 6.1.0

Itoukee avatar Feb 02 '23 10:02 Itoukee

I also have the same issue. From passport.use(User.createStrategy());, I'm having the trouble to use "email" in replace of "username" for user authentification.

Version: 8.0.0

Shenghan0329 avatar Apr 17 '23 03:04 Shenghan0329

TLDR Try: passport.use('user-local', User.createStrategy());

Brief:

Bit of a strange bug. I have multi user login, and it needs passport.use('user-local', new LocalStrategy(User.authenticate())); for the first 2 User types, but for the 3rd I need to use passport.use('user-local', User.createStrategy());

Context:

Plugin passport-local-mongoose to "userSchema" - set usernameField to "email"

In app.js For first and second user login use passport.use(User.createStrategy()); passport.use('user-local', new LocalStrategy(User.authenticate())); For third user login use passport.use('user-local', User.createStrategy());

Incoming form contains "email" and "password" fields

Authenticate with passport.authenticate('user-local')

Using v6.3.0

u1k0g avatar Nov 11 '23 06:11 u1k0g