Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make passportjs local username be case insensitive?

Right now I'm using passportjs (local) for authentication on my site, but the username field is case sensitive, is there a way to change this? I thought about making the username all lowercase at the moment of registration, but that doesn't seem like a very efficient choice and still doesn't let me make log in case insensitive.

router.post("/login", middleware.isNotLoggedIn, passport.authenticate("local", {
successRedirect: "",
failureRedirect: "/login"
}), function(req, res){
    res.redirect("back");
});
like image 453
Danyx Avatar asked Sep 08 '16 18:09

Danyx


People also ask

What is local in passport authenticate?

The local authentication strategy authenticates users using a username and password. The strategy requires a verify callback, which accepts these credentials and calls done providing a user.

What does passport session () do?

To sum it up, passport. serializeUser() saves the user inside the session which was earlier created by express-session middleware. Once the user is saved inside the session, the session itself also will get stored inside the database (only if we opt to).

What is strategy in Passportjs?

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.


1 Answers

I had the same problem myself. Seems like passport should have an official answer to this. Simplest way it to make a middleware. This middleware would need to be in TWO places. First in the register function so that the username is saved in lowercase. Then in the Login function as shown below for an example. Good luck!

  function usernameToLowerCase(req, res, next){
            req.body.username = req.body.username.toLowerCase();
            next();
        }
  router.post("/login", usernameToLowerCase, passport.authenticate("local", {
        successRedirect: "/",
        failureRedirect: "/login"
    }), function (req, res) {
    });
like image 95
AndrewLeonardi Avatar answered Sep 30 '22 04:09

AndrewLeonardi