From 32962aa1e84c09d87141440a1f04cbd1659b3336 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 11 Mar 2019 19:35:22 +0000 Subject: [PATCH] External User Fix (#2220) * fix: fixed bug with user upserting * chore: version bump --- package.json | 2 +- services/users.js | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a866f69b1..1c39edab2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "talk", - "version": "4.8.1", + "version": "4.8.2", "description": "A better commenting experience from Mozilla, The New York Times, and the Washington Post. https://coralproject.net", "main": "app.js", "private": true, diff --git a/services/users.js b/services/users.js index e85ed67a2..3aff5e557 100644 --- a/services/users.js +++ b/services/users.js @@ -60,6 +60,7 @@ async function upsertUser( shouldSetDisplayName = false ) { let user = await User.findOne({ + id, profiles: { $elemMatch: { id, @@ -80,6 +81,7 @@ async function upsertUser( // The user was not found, lets create them! user = new User({ + id, username, lowercaseUsername: username.toLowerCase(), profiles: [{ id, provider }], @@ -798,8 +800,13 @@ class Users { * @param {Object} token a jwt token used to sign in the user */ static async findOrCreateByIDToken(id, token) { + // FIXME: (wyattjoh) adding the `{ 'profiles.id': id }` search condition is + // a workaround for the `upsertExternalUser` bug where the ID being set was + // incorrect. Patch the user creation functions to adapt to the correct + // behavior. Thankfully, this is currently still covered by an index. + // Try to get the user. - let user = await User.findOne({ id }); + let user = await User.findOne({ $or: [{ id }, { 'profiles.id': id }] }); // If the user was not found, try to look it up. if (user === null) {