From da9b2f66fe1f0c395397eef564a4f047e55ef711 Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Fri, 15 Jul 2022 20:28:42 -0400 Subject: [PATCH] Safer connection updates --- src/database.js | 29 +++++++------------------- src/services/api/routes/v1/register.js | 4 +--- src/services/nnid/routes/people.js | 4 +--- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/database.js b/src/database.js index 90758fa..b636218 100644 --- a/src/database.js +++ b/src/database.js @@ -177,7 +177,6 @@ function getServerByTitleId(titleId, accessMode) { } async function addUserConnection(pnid, data, type) { - // Add more connections later? if (type === 'discord') { return await addUserConnectionDiscord(pnid, data); } @@ -192,16 +191,11 @@ async function addUserConnectionDiscord(pnid, data) { }; } - // Support older documents - await PNID.updateOne({ - pid: pnid.get('pid') - }, { + await PNID.updateOne({ pid: pnid.get('pid') }, { $set: { - connections: { - discord: data - } + 'connections.discord': data } - }, { upsert: true }); + }); return { app: 'api', @@ -217,20 +211,13 @@ async function removeUserConnection(pnid, type) { } async function removeUserConnectionDiscord(pnid) { - // Support older documents - await PNID.updateOne({ - pid: pnid.get('pid') - }, { + await PNID.updateOne({ pid: pnid.get('pid') }, { $set: { - connections: { - discord: { - id: '', - access_token: '', - refresh_token: '' - } - } + 'connections.discord.id': '', + 'connections.discord.access_token': '', + 'connections.discord.refresh_token': '' } - }, { upsert: true }); + }); return { app: 'api', diff --git a/src/services/api/routes/v1/register.js b/src/services/api/routes/v1/register.js index 4a7311f..51a3ef5 100644 --- a/src/services/api/routes/v1/register.js +++ b/src/services/api/routes/v1/register.js @@ -296,9 +296,7 @@ router.post('/', async (request, response) => { // NN with a NNID will always use the NNID PID // even if the provided NEX PID is different // To fix this we make them the same PID - await NEXAccount.updateOne({ - pid: newNEXAccount.get('pid') - }, { + await NEXAccount.updateOne({ pid: newNEXAccount.get('pid') }, { owning_pid: newNEXAccount.get('pid') }); diff --git a/src/services/nnid/routes/people.js b/src/services/nnid/routes/people.js index 4271150..ebc058a 100644 --- a/src/services/nnid/routes/people.js +++ b/src/services/nnid/routes/people.js @@ -134,9 +134,7 @@ router.post('/', ratelimit, deviceCertificateMiddleware, async (request, respons // NN with a NNID will always use the NNID PID // even if the provided NEX PID is different // To fix this we make them the same PID - await NEXAccount.updateOne({ - pid: newNEXAccount.get('pid') - }, { + await NEXAccount.updateOne({ pid: newNEXAccount.get('pid') }, { owning_pid: newNEXAccount.get('pid') });