1

I was working with express-validator when i encountered that i used normalize email for validation of email while signing up so i stored the normalized email to server.

Validation code:

router.post(
  "/signup",
  [
    check("name").not().isEmpty(),
    check("email").normalizeEmail().isEmail(),
    check("password").isLength({ min: 6, max: 15 }),
  ],
  userController.signupUser
);

Input email: abc.123@mail.com

normalized email: abc123@gmail.com (storing in db)

Now the problem is when working on login the input email doesn't matches and shows invalid credentials.

Vivek Sharma
  • 531
  • 6
  • 20

3 Answers3

3

You just need to pass options inside normalizeEmail ():

normalizeEmail ({"gmail_remove_dots": false "})
piet.t
  • 11,718
  • 21
  • 43
  • 52
  • Good solution, but you made a typo `{gmail_remove_dots: false }` and for forgot to include source https://github.com/validatorjs/validator.js#sanitizers – Max O. Apr 17 '22 at 14:33
0

After researching and observing I noticed that the code which was used at the time of signing up can be used to normalize email at the time of login, I just had to add:

 router.post("/login",[check('email').normalizeEmail()], userController.loginUser);

After adding the email was converting to normalized one and can be used directly from requests.

Vivek Sharma
  • 531
  • 6
  • 20
-1

You cannot remove any . or _ since it's part of the user's e-mail.

Here and example of validating an email address

  • 1
    can you suggest a solution for the library i am using,because i intentionally want to have a normalized email at backend. – Vivek Sharma Jun 27 '20 at 20:27
  • I give a short read on the [docs](https://express-validator.github.io/docs/validation-chain-api.html) it seems you shoud only use `isEmail()` not `normalizeEmail()`. I recommend to simple use [toLowerCase()](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase) to get it lower case – GersonSalvador Jun 29 '20 at 00:06
  • but i want to use normalize email so that some other person cannot make account with email of same signature. – Vivek Sharma Jul 03 '20 at 10:05
  • Well, that should be done on your table making it unique or checking it manually. – GersonSalvador Jul 03 '20 at 22:38