Open naossoan opened 2 years ago
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?
Up on this issue ? I seem to got it too and I don't see any fixes version : 6.1.0
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
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
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:if you want to use a different field for username.
It says directly below this:
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:
NOT
passport.use(User.createStrategy());
ORpassport.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()));