Skip to content

Commit

Permalink
Merge pull request #19 from js-accounts/no-auto-convert
Browse files Browse the repository at this point in the history
Added a flag that allows for user collection to have simple string ids
  • Loading branch information
davidyaha authored and Aetherall committed Mar 13, 2018
1 parent adae171 commit eea8062
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 30 deletions.
9 changes: 7 additions & 2 deletions packages/accounts-mongo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"flow": "flow; test $? -eq 0 -o $? -eq 2",
"flow:check": "flow check",
"prepare": "yarn compile",
"test": "yarn lint && yarn testonly",
"pretest": "yarn compile",
"test": "yarn testonly",
"posttest": "yarn lint",
"test-ci": "yarn lint && yarn coverage",
"testonly": "jest",
"lint": "eslint src",
Expand All @@ -21,7 +23,10 @@
},
"jest": {
"testEnvironment": "node",
"testRegex": "(/.*.(test|spec)).(js|jsx)$"
"testRegex": "(/.*.(test|spec)).(js|jsx)$",
"testPathDirs": [
"src"
]
},
"repository": {
"type": "git",
Expand Down
25 changes: 17 additions & 8 deletions packages/accounts-mongo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export type MongoOptionsType = {
timestamps: {
createdAt: string,
updatedAt: string,
}
},
convertUserIdToMongoObjectId: boolean
};

export type MongoUserObjectType = {
Expand Down Expand Up @@ -54,6 +55,7 @@ class Mongo {
createdAt: 'createdAt',
updatedAt: 'updatedAt',
},
convertUserIdToMongoObjectId: true,
};
this.options = { ...defaultOptions, ...options };
if (!db) {
Expand Down Expand Up @@ -93,7 +95,8 @@ class Mongo {
}

async findUserById(userId: string): Promise<?UserObjectType> {
const user = await this.collection.findOne({ _id: toMongoID(userId) });
const _id = this.options.convertUserIdToMongoObjectId ? toMongoID(userId) : userId;
const user = await this.collection.findOne({ _id });
if (user) {
user.id = user._id;
}
Expand Down Expand Up @@ -133,15 +136,17 @@ class Mongo {
}

async findPasswordHash(userId: string): Promise<?string> {
const user = await this.findUserById(toMongoID(userId));
const id = this.options.convertUserIdToMongoObjectId ? toMongoID(userId) : userId;
const user = await this.findUserById(id);
if (user) {
return user.services.password.bcrypt;
}
return null;
}

async addEmail(userId: string, newEmail: string, verified: boolean): Promise<void> {
const ret = await this.collection.update({ _id: toMongoID(userId) }, {
const _id = this.options.convertUserIdToMongoObjectId ? toMongoID(userId) : userId;
const ret = await this.collection.update({ _id }, {
$addToSet: {
emails: {
address: newEmail.toLowerCase(),
Expand All @@ -156,7 +161,8 @@ class Mongo {
}

async removeEmail(userId: string, email: string): Promise<void> {
const ret = await this.collection.update({ _id: toMongoID(userId) }, {
const _id = this.options.convertUserIdToMongoObjectId ? toMongoID(userId) : userId;
const ret = await this.collection.update({ _id }, {
$pull: { emails: { address: email.toLowerCase() } },
$set: { [this.options.timestamps.updatedAt]: Date.now() },
});
Expand All @@ -166,7 +172,8 @@ class Mongo {
}

async setUsername(userId: string, newUsername: string): Promise<void> {
const ret = await this.collection.update({ _id: toMongoID(userId) }, {
const _id = this.options.convertUserIdToMongoObjectId ? toMongoID(userId) : userId;
const ret = await this.collection.update({ _id }, {
$set: {
username: newUsername,
[this.options.timestamps.updatedAt]: Date.now(),
Expand All @@ -178,7 +185,8 @@ class Mongo {
}

async setPasssword(userId: string, newPassword: string): Promise<void> {
const ret = await this.collection.update({ _id: toMongoID(userId) }, {
const _id = this.options.convertUserIdToMongoObjectId ? toMongoID(userId) : userId;
const ret = await this.collection.update({ _id }, {
$set: {
'services.password.bcrypt': await encryption.hashPassword(newPassword),
[this.options.timestamps.updatedAt]: Date.now(),
Expand All @@ -190,7 +198,8 @@ class Mongo {
}

async setProfile(userId: string, profile: Object): Promise<Object> {
await this.collection.update({ _id: toMongoID(userId) }, {
const _id = this.options.convertUserIdToMongoObjectId ? toMongoID(userId) : userId;
await this.collection.update({ _id }, {
$set: {
profile,
[this.options.timestamps.updatedAt]: Date.now(),
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 @@ -64,6 +64,15 @@ describe('Mongo', () => {
expect(err.message).toEqual('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
}
});

it('should not auto convert to mongo object id when users collection has string ids', async () => {
const mongoWithStringIds = new Mongo(db, { convertUserIdToMongoObjectId: false });
const mockFindOne = jest.fn();
mongoWithStringIds.collection.findOne = mockFindOne;
await mongoWithStringIds.findUserById('589871d1c9393d445745a57c');

expect(mockFindOne.mock.calls[0][0]).toHaveProperty('_id', '589871d1c9393d445745a57c');
});
});

describe('#constructor', () => {
Expand Down
87 changes: 67 additions & 20 deletions packages/accounts-mongo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
# yarn lockfile v1


"@accounts/common@^0.0.2":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@accounts/common/-/common-0.0.2.tgz#be040983e5d36b6ce0471dc3c85502274a8792de"
"@accounts/common@^0.0.9":
version "0.0.9"
resolved "https://registry.yarnpkg.com/@accounts/common/-/common-0.0.9.tgz#5aa8347b81c68845331409e4a99eaf68b924ec27"
dependencies:
apollo-errors "^1.2.1"
lodash "^4.16.4"

"@accounts/server@^0.0.2":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@accounts/server/-/server-0.0.2.tgz#d4bd5c2171a43fa86c0b623e9a940f1e102e5091"
"@accounts/server@^0.0.9":
version "0.0.9"
resolved "https://registry.yarnpkg.com/@accounts/server/-/server-0.0.9.tgz#dddcb4fbaa7ac126aca1f0c58b40c9d84820a851"
dependencies:
"@accounts/common" "^0.0.2"
apollo-errors "^1.2.1"
"@accounts/common" "^0.0.9"
babel-polyfill "^6.23.0"
bcryptjs "^2.4.0"
crypto "^0.0.3"
emailjs "^1.0.8"
jsonwebtoken "^7.2.1"
jwt-decode "^2.1.0"
lodash "^4.16.4"
Expand Down Expand Up @@ -52,6 +53,14 @@ acorn@^4.0.1:
version "4.0.4"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.4.tgz#17a8d6a7a6c4ef538b814ec9abac2779293bf30a"

addressparser@^0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/addressparser/-/addressparser-0.3.2.tgz#59873f35e8fcf6c7361c10239261d76e15348bb2"

addressparser@~0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/addressparser/-/addressparser-0.2.1.tgz#d11a5b2eeda04cfefebdf3196c10ae13db6cd607"

ajv-keywords@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.2.0.tgz#676c4f087bfe1e8b12dca6fda2f3c74f417b099c"
Expand Down Expand Up @@ -98,12 +107,6 @@ anymatch@^1.3.0:
arrify "^1.0.0"
micromatch "^2.1.5"

apollo-errors@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/apollo-errors/-/apollo-errors-1.2.1.tgz#08b6de091444fd73d73bb5674a3137c312b25ffb"
dependencies:
es6-error "^4.0.0"

append-transform@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/append-transform/-/append-transform-0.4.0.tgz#d76ebf8ca94d276e247a36bad44a4b74ab611991"
Expand Down Expand Up @@ -802,6 +805,14 @@ babel-polyfill@^6.22.0:
core-js "^2.4.0"
regenerator-runtime "^0.10.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:
babel-runtime "^6.22.0"
core-js "^2.4.0"
regenerator-runtime "^0.10.0"

babel-preset-es2015@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-preset-es2015/-/babel-preset-es2015-6.22.0.tgz#af5a98ecb35eb8af764ad8a5a05eb36dc4386835"
Expand Down Expand Up @@ -1072,6 +1083,10 @@ buffer@^4.9.0:
ieee754 "^1.1.4"
isarray "^1.0.0"

bufferjs@=1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/bufferjs/-/bufferjs-1.1.0.tgz#095ffa39c5e6b40a2178a1169c9effc584a73201"

builtin-modules@^1.0.0, builtin-modules@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"
Expand Down Expand Up @@ -1295,6 +1310,10 @@ [email protected]:
ripemd160 "0.2.0"
sha.js "2.2.6"

crypto@^0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/crypto/-/crypto-0.0.3.tgz#470a81b86be4c5ee17acc8207a1f5315ae20dbb0"

[email protected], "cssom@>= 0.3.0 < 0.4.0":
version "0.3.1"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.1.tgz#c9e37ef2490e64f6d1baa10fda852257082c25d3"
Expand Down Expand Up @@ -1410,10 +1429,27 @@ [email protected]:
base64url "^2.0.0"
safe-buffer "^5.0.1"

emailjs@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/emailjs/-/emailjs-1.0.8.tgz#d4240db7670dc78aff97352092d8460edc130f66"
dependencies:
addressparser "^0.3.2"
mimelib "0.2.14"
moment "= 2.11.2"
starttls "1.0.1"
optionalDependencies:
bufferjs "=1.1.0"

emojis-list@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"

encoding@~0.1:
version "0.1.12"
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb"
dependencies:
iconv-lite "~0.4.13"

enhanced-resolve@~0.9.0:
version "0.9.1"
resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-0.9.1.tgz#4d6e689b3725f86090927ccc86cd9f1635b89e2e"
Expand Down Expand Up @@ -1458,10 +1494,6 @@ es5-ext@^0.10.7, es5-ext@^0.10.8, es5-ext@~0.10.11, es5-ext@~0.10.2, es5-ext@~0.
es6-iterator "2"
es6-symbol "~3.1"

es6-error@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/es6-error/-/es6-error-4.0.1.tgz#eeb3e280f57e2ec48d72a9fccaf6247d3c1f5719"

es6-iterator@2:
version "2.0.0"
resolved "https://registry.yarnpkg.com/es6-iterator/-/es6-iterator-2.0.0.tgz#bd968567d61635e33c0b80727613c9cb4b096bac"
Expand Down Expand Up @@ -2030,7 +2062,7 @@ [email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-0.0.1.tgz#3f91365cabe60b77ed0ebba24b454e3e09d95a82"

[email protected], iconv-lite@^0.4.13:
[email protected], iconv-lite@^0.4.13, iconv-lite@~0.4.13:
version "0.4.13"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2"

Expand Down Expand Up @@ -2866,6 +2898,13 @@ mime-types@^2.1.11, mime-types@^2.1.12, mime-types@~2.1.7:
dependencies:
mime-db "~1.25.0"

[email protected]:
version "0.2.14"
resolved "https://registry.yarnpkg.com/mimelib/-/mimelib-0.2.14.tgz#2a1aa724bd190b85bd526e6317ab6106edfd6831"
dependencies:
addressparser "~0.2.0"
encoding "~0.1"

minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774"
Expand All @@ -2890,6 +2929,10 @@ [email protected]:
version "2.17.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.17.1.tgz#fed9506063f36b10f066c8b59a144d7faebe1d82"

"moment@= 2.11.2":
version "2.11.2"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.11.2.tgz#87968e5f95ac038c2e42ac959c75819cd3f52901"

[email protected]:
version "2.1.7"
resolved "https://registry.yarnpkg.com/mongodb-core/-/mongodb-core-2.1.7.tgz#6a27909b98142ef2508d924c274969008954fa29"
Expand Down Expand Up @@ -3649,6 +3692,10 @@ sshpk@^1.7.0:
jsbn "~0.1.0"
tweetnacl "~0.14.0"

[email protected]:
version "1.0.1"
resolved "https://registry.yarnpkg.com/starttls/-/starttls-1.0.1.tgz#e6081c25de6b178f5a75f8f271c1487449183b42"

stream-browserify@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-2.0.1.tgz#66266ee5f9bdb9940a4e4514cafb43bb71e5c9db"
Expand Down

0 comments on commit eea8062

Please sign in to comment.