Skip to content

Commit ba92e01

Browse files
committed
Fix username/email case issue in login/signup
1 parent ab7f427 commit ba92e01

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

server/controllers/user.controller.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ export function findUserByUsername(username, cb) {
2727
const EMAIL_VERIFY_TOKEN_EXPIRY_TIME = Date.now() + (3600000 * 24); // 24 hours
2828

2929
export function createUser(req, res, next) {
30+
let { username, email } = req.body;
31+
const { password } = req.body;
32+
username = username.toLowerCase();
33+
email = email.toLowerCase();
3034
random((tokenError, token) => {
3135
const user = new User({
32-
username: req.body.username,
33-
email: req.body.email,
34-
password: req.body.password,
36+
username,
37+
email,
38+
password,
3539
verified: User.EmailConfirmation.Sent,
3640
verifiedToken: token,
3741
verifiedTokenExpires: EMAIL_VERIFY_TOKEN_EXPIRY_TIME,
@@ -40,8 +44,8 @@ export function createUser(req, res, next) {
4044
User.findOne(
4145
{
4246
$or: [
43-
{ email: req.body.email },
44-
{ username: req.body.username }
47+
{ email },
48+
{ username }
4549
]
4650
},
4751
(err, existingUser) => {
@@ -51,7 +55,7 @@ export function createUser(req, res, next) {
5155
}
5256

5357
if (existingUser) {
54-
const fieldInUse = existingUser.email === req.body.email ? 'Email' : 'Username';
58+
const fieldInUse = existingUser.email === email ? 'Email' : 'Username';
5559
res.status(422).send({ error: `${fieldInUse} is in use` });
5660
return;
5761
}
@@ -77,8 +81,8 @@ export function createUser(req, res, next) {
7781

7882
mail.send(mailOptions, (mailErr, result) => { // eslint-disable-line no-unused-vars
7983
res.json({
80-
email: req.user.email,
81-
username: req.user.username,
84+
email,
85+
username,
8286
preferences: req.user.preferences,
8387
verified: req.user.verified,
8488
id: req.user._id

0 commit comments

Comments
 (0)