I am making an API in which I am taking username and password as req.body so that I can authenticate user.
(In short login functionality)
User can enter email or username or mobile number. At backend I want to normalize username if it is email. I am doing so because I am normalizing email at the time of Registration, therefore I need to check it with normalized email.
Want to do something like this: (A short pseudo-code)
username <- take input from request
check if username is email:
if yes then *sanitize* username
My Login Rest API For Now:
// Login user
router.post(
"/login",
[
check("username").not().isEmpty().escape(),
check("password").not().isEmpty().escape(),
],
(req, res, next) => {
// Finds validation errors and return error object
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
let { username, password } = req.body;
// Do authentication part
}
);
The above code will work fine if the input is a username but I want to normalize email also.
Database content looks like this
user : [
{
username: 'SOME_MOBILE',
password: 'SLDUS##$##KDJ'
},
{
username: 'SOME_EMAIL',
password: 'SLDU$%%##$2342423DJ'
},
{
username: 'my_username',
password: 'SLDUS##$#fssKDJ'
}
]
Please help!
Thanks