Skip to content

Commit ef4a8d7

Browse files
committed
Use MongoDB Regexes to query case insensitive username/password
1 parent 86e299c commit ef4a8d7

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

server/config/passport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ passport.deserializeUser((id, done) => {
2424
* Sign in using Email/Username and Password.
2525
*/
2626
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
27-
User.findByMailOrName(email.toLowerCase())
27+
User.findByMailOrName(email)
2828
.then((user) => { // eslint-disable-line consistent-return
2929
if (!user) {
3030
return done(null, false, { msg: `Email ${email} not found.` });

server/controllers/user.controller.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import crypto from 'crypto';
22
import async from 'async';
3+
import escapeStringRegexp from 'escape-string-regexp';
34

45
import User from '../models/user';
56
import mail from '../utils/mail';
@@ -41,12 +42,11 @@ export function findUserByUsername(username, cb) {
4142
export function createUser(req, res, next) {
4243
const { username, email } = req.body;
4344
const { password } = req.body;
44-
const usernameLowerCase = username.toLowerCase();
4545
const emailLowerCase = email.toLowerCase();
4646
const EMAIL_VERIFY_TOKEN_EXPIRY_TIME = Date.now() + (3600000 * 24); // 24 hours
4747
random((tokenError, token) => {
4848
const user = new User({
49-
username: usernameLowerCase,
49+
username: username,
5050
email: emailLowerCase,
5151
password,
5252
verified: User.EmailConfirmation.Sent,
@@ -57,8 +57,8 @@ export function createUser(req, res, next) {
5757
User.findOne(
5858
{
5959
$or: [
60-
{ email: { $in: [ email, emailLowerCase ]} },
61-
{ username: { $in: [ username, usernameLowerCase ]} }
60+
{ email: new RegExp(`^${escapeStringRegexp(email)}$`, 'i') },
61+
{ username: new RegExp(`^${escapeStringRegexp(username)}$`, 'i') }
6262
]
6363
},
6464
(err, existingUser) => {
@@ -106,7 +106,7 @@ export function duplicateUserCheck(req, res) {
106106
const checkType = req.query.check_type;
107107
const value = req.query[checkType];
108108
const query = {};
109-
query[checkType] = value;
109+
query[checkType] = new RegExp(`^${escapeStringRegexp(value)}$`, 'i');
110110
User.findOne(query, (err, user) => {
111111
if (user) {
112112
return res.json({
@@ -151,7 +151,7 @@ export function resetPasswordInitiate(req, res) {
151151
async.waterfall([
152152
random,
153153
(token, done) => {
154-
User.findOne({ email: req.body.email }, (err, user) => {
154+
User.findOne({ email: req.body.email.toLowerCase() }, (err, user) => {
155155
if (!user) {
156156
res.json({ success: true, message: 'If the email is registered with the editor, an email has been sent.' });
157157
return;
@@ -281,7 +281,7 @@ export function updatePassword(req, res) {
281281
}
282282

283283
export function userExists(username, callback) {
284-
User.findOne({ username }, (err, user) => (
284+
User.findOne({ username: new RegExp(`^${escapeStringRegexp(username)}$`, 'i') }, (err, user) => (
285285
user ? callback(true) : callback(false)
286286
));
287287
}

server/models/user.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mongoose from 'mongoose';
2+
import escapeStringRegexp from 'escape-string-regexp';
23

34
const bcrypt = require('bcrypt-nodejs');
45

@@ -144,9 +145,9 @@ userSchema.methods.findMatchingKey = function findMatchingKey(candidateKey, cb)
144145
userSchema.statics.findByMailOrName = function findByMailOrName(email) {
145146
const query = {
146147
$or: [{
147-
email,
148+
email: new RegExp(`^${escapeStringRegexp(email)}$`, 'i'),
148149
}, {
149-
username: email,
150+
username: new RegExp(`^${escapeStringRegexp(email)}$`, 'i'),
150151
}],
151152
};
152153
return this.findOne(query).exec();

0 commit comments

Comments
 (0)