From 306bc0646f3d3470ab991743663a47f47f96219d Mon Sep 17 00:00:00 2001 From: Jonas Lochmann Date: Mon, 14 Oct 2019 00:00:00 +0000 Subject: [PATCH] Fix invalidating auth token when device reports uninstall --- src/function/cleanup/delete-families.ts | 25 ++++++++++++++++---- src/function/device/report-device-removed.ts | 4 +++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/function/cleanup/delete-families.ts b/src/function/cleanup/delete-families.ts index 028c167..5174238 100644 --- a/src/function/cleanup/delete-families.ts +++ b/src/function/cleanup/delete-families.ts @@ -15,6 +15,7 @@ * along with this program. If not, see . */ +import { difference } from 'lodash' import * as Sequelize from 'sequelize' import { Database } from '../../database' @@ -129,11 +130,25 @@ export async function deleteFamilies ({ database, familiyIds }: { // olddevice if (oldDeviceAuthTokens.length > 0) { - await database.oldDevice.bulkCreate( - oldDeviceAuthTokens.map((item) => ({ - deviceAuthToken: item - })) - ) + const knownOldDeviceAuthTokens = await database.oldDevice.findAll({ + where: { + deviceAuthToken: { + [Sequelize.Op.in]: oldDeviceAuthTokens + } + }, + transaction + }).map((item) => item.deviceAuthToken) + + const oldDeviceAuthTokensToAdd = difference(oldDeviceAuthTokens, knownOldDeviceAuthTokens) + + if (oldDeviceAuthTokensToAdd.length > 0) { + await database.oldDevice.bulkCreate( + oldDeviceAuthTokensToAdd.map((item) => ({ + deviceAuthToken: item + })), + { transaction } + ) + } } // family diff --git a/src/function/device/report-device-removed.ts b/src/function/device/report-device-removed.ts index 43e36d4..f5c2cde 100644 --- a/src/function/device/report-device-removed.ts +++ b/src/function/device/report-device-removed.ts @@ -35,6 +35,8 @@ export async function reportDeviceRemoved ({ database, deviceAuthToken, websocke }) if (deviceEntry) { + const currentAuthToken = deviceEntry.deviceAuthToken + deviceEntry.didDeviceReportUninstall = true deviceEntry.deviceAuthToken = generateAuthToken() // invalidiate the token deviceEntry.save({ transaction }) @@ -51,7 +53,7 @@ export async function reportDeviceRemoved ({ database, deviceAuthToken, websocke // add to old devices await database.oldDevice.create({ - deviceAuthToken: deviceEntry.deviceAuthToken + deviceAuthToken: currentAuthToken }, { transaction })