Fix username/email case issue in login/signup

This commit is contained in:
shakti97 2020-03-09 01:39:01 +05:30
parent ab7f4279bd
commit ba92e01762

View file

@ -27,11 +27,15 @@ export function findUserByUsername(username, cb) {
const EMAIL_VERIFY_TOKEN_EXPIRY_TIME = Date.now() + (3600000 * 24); // 24 hours const EMAIL_VERIFY_TOKEN_EXPIRY_TIME = Date.now() + (3600000 * 24); // 24 hours
export function createUser(req, res, next) { export function createUser(req, res, next) {
let { username, email } = req.body;
const { password } = req.body;
username = username.toLowerCase();
email = email.toLowerCase();
random((tokenError, token) => { random((tokenError, token) => {
const user = new User({ const user = new User({
username: req.body.username, username,
email: req.body.email, email,
password: req.body.password, password,
verified: User.EmailConfirmation.Sent, verified: User.EmailConfirmation.Sent,
verifiedToken: token, verifiedToken: token,
verifiedTokenExpires: EMAIL_VERIFY_TOKEN_EXPIRY_TIME, verifiedTokenExpires: EMAIL_VERIFY_TOKEN_EXPIRY_TIME,
@ -40,8 +44,8 @@ export function createUser(req, res, next) {
User.findOne( User.findOne(
{ {
$or: [ $or: [
{ email: req.body.email }, { email },
{ username: req.body.username } { username }
] ]
}, },
(err, existingUser) => { (err, existingUser) => {
@ -51,7 +55,7 @@ export function createUser(req, res, next) {
} }
if (existingUser) { if (existingUser) {
const fieldInUse = existingUser.email === req.body.email ? 'Email' : 'Username'; const fieldInUse = existingUser.email === email ? 'Email' : 'Username';
res.status(422).send({ error: `${fieldInUse} is in use` }); res.status(422).send({ error: `${fieldInUse} is in use` });
return; return;
} }
@ -77,8 +81,8 @@ export function createUser(req, res, next) {
mail.send(mailOptions, (mailErr, result) => { // eslint-disable-line no-unused-vars mail.send(mailOptions, (mailErr, result) => { // eslint-disable-line no-unused-vars
res.json({ res.json({
email: req.user.email, email,
username: req.user.username, username,
preferences: req.user.preferences, preferences: req.user.preferences,
verified: req.user.verified, verified: req.user.verified,
id: req.user._id id: req.user._id