Skip to content

Commit

Permalink
Merge pull request #23 from mdemri/caseinsensitiveusername
Browse files Browse the repository at this point in the history
case insensitive user name query
  • Loading branch information
pradel authored and Aetherall committed Mar 11, 2018
1 parent 999405f commit b1a17ed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/accounts-mongo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"eslint": "^3.14.1",
"eslint-config-airbnb": "^14.0.0",
"eslint-config-airbnb-base": "^11.0.1",
"eslint-plugin-flowtype": "^2.30.0",
"eslint-plugin-flowtype": "^2.33.0",
"eslint-plugin-import": "^2.0.1",
"eslint-plugin-jsx-a11y": "^3.0.2",
"eslint-plugin-react": "^6.9.0",
Expand Down
7 changes: 5 additions & 2 deletions packages/accounts-mongo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export type MongoOptionsType = {
createdAt: string,
updatedAt: string,
},
convertUserIdToMongoObjectId: boolean
convertUserIdToMongoObjectId: boolean,
caseSensitiveUserName: boolean
};

export type MongoUserObjectType = {
Expand Down Expand Up @@ -59,6 +60,7 @@ class Mongo {
updatedAt: 'updatedAt',
},
convertUserIdToMongoObjectId: true,
caseSensitiveUserName: true,
};
this.options = { ...defaultOptions, ...options };
if (!db) {
Expand Down Expand Up @@ -115,7 +117,8 @@ class Mongo {
}

async findUserByUsername(username: string): Promise<?UserObjectType> {
const user = await this.collection.findOne({ username });
const filter = this.options.caseSensitiveUserName ? { username } : { username: { $regex: new RegExp(username, 'i') } };
const user = await this.collection.findOne(filter);
if (user) {
user.id = user._id;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/accounts-mongo/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ describe('Mongo', () => {
expect(ret).not.toBeTruthy();
});

it('should return username for case insensitive query', async () => {
const mongoWithOptions = new Mongo(db, { caseSensitiveUserName: false });
const ret = await mongoWithOptions.findUserByUsername(user.username.toUpperCase());
await delay(10);
expect(ret).toBeTruthy();
expect(ret._id).toBeTruthy();
expect(ret.id).toBeTruthy();
});

it('should return user', async () => {
const ret = await mongo.findUserByUsername(user.username);
await delay(10);
Expand Down
16 changes: 4 additions & 12 deletions packages/accounts-mongo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -797,15 +797,7 @@ babel-plugin-transform-strict-mode@^6.22.0:
babel-runtime "^6.22.0"
babel-types "^6.22.0"

babel-polyfill@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.22.0.tgz#1ac99ebdcc6ba4db1e2618c387b2084a82154a3b"
dependencies:
babel-runtime "^6.22.0"
core-js "^2.4.0"
regenerator-runtime "^0.10.0"

babel-polyfill@^6.23.0:
babel-polyfill@^6.22.0, babel-polyfill@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.23.0.tgz#8364ca62df8eafb830499f699177466c3b03499d"
dependencies:
Expand Down Expand Up @@ -1592,9 +1584,9 @@ eslint-module-utils@^2.0.0:
debug "2.2.0"
pkg-dir "^1.0.0"

eslint-plugin-flowtype@^2.30.0:
version "2.30.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-flowtype/-/eslint-plugin-flowtype-2.30.0.tgz#3054a265f9c8afe3046c3d41b72d32a736f9b4ae"
eslint-plugin-flowtype@^2.33.0:
version "2.33.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-flowtype/-/eslint-plugin-flowtype-2.33.0.tgz#b2783814ed2ddcf729953b8f65ff73c90cabee4b"
dependencies:
lodash "^4.15.0"

Expand Down

0 comments on commit b1a17ed

Please sign in to comment.