Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usernameField settings outlined in the docs do not work #360

Open
naossoan opened this issue Aug 3, 2022 · 4 comments
Open

usernameField settings outlined in the docs do not work #360

naossoan opened this issue Aug 3, 2022 · 4 comments

Comments

@naossoan
Copy link

naossoan commented Aug 3, 2022

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()));

@delivey
Copy link

delivey commented Nov 7, 2022

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?

@Itoukee
Copy link

Itoukee commented Feb 2, 2023

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

@Shenghan0329
Copy link

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

@u1k0g
Copy link

u1k0g commented Nov 11, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants