Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

602
Vistas
How to change passport to use email instead of username for authenticating?

I wanna use user's email and password for logging in. But in passport you should use username.

I've tried many examples about how to do that. But non worked. The real problem is when application wants to save the record into database. Although I have change the code to look for email not username field, this is the final record saved to database:

'name': 'joseph',
'username' : 'example@gmail.com',
.
.

But I don't want the email to be save as username. And when I change it with mongoose after user registration it would be correct but again the problem will come when app wants to authenticate user.

It will look for username field in database not email. Because user will provide email as the username, login will fail.

Code:

app.get("/register", function(req, res) {
   var newUser = new User({
    username :  req.body.username,
    email :  req.body.email,
    name: req.body.name
  });

  User.register(newUser, req.body.password, function(err, user) {
    if (err) {
      req.flash('error', "An error ocurred, please try again.");
      return res.redirect('back');
    } else {
      passport.authenticate("local")(req, res, function() {
        req.flash("success", "Welcome");
        res.redirect("/admin");
      });
    }
  });

});

This code register user the way I want, but the problem is about login:

router.post('/login', middleware.outLoggedIn, passport.authenticate("local", {
    successRedirect: "/admin",
    failureRedirect: "/login",
    failureFlash: "Invalid username or password",
    successFlash: "Welcome!"
}));

It always fails, because it will get user email provided in login form, then it'll compare it with the one in database. And since I have saved the user with email and username, it will compare the provided email in login form with username field in database. AND FAIL.

This is the user in databse:

{
    "_id" : ObjectId("5901cc5c0256ed17b4d61960"),
    "salt" : "---",
    "hash" : "---",
    "username" : "joseph320",
    "email" : "example@gmail.com",
    "name" : "joseph",
    "__v" : 0
}

(Login form requires email and password)

I have also done this:

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

I really appreciate it if you can show me how to fix that.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Can you provide more of your code? Some context is important here, such as which npm modules you're using to read and write to your database.

** If you are using passport-local-mongoose

You said you are using mongoose, and it looks like you might be using passport-local-mongoose on top of that. If this is the case, you can look at the options for passport-local-mongoose. This also includes a usernameField option, which indicates where you want the "username" to be stored. The usage for that would look like this:

User.plugin(passportLocalMongoose, {usernameField: 'email'})
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda