From d1b81b6afe9d96e72b747eb45832c7860d252156 Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Tue, 4 Apr 2017 14:38:54 -0700 Subject: [PATCH] [client-app] Prevent from making any requests when NylasID isn't present Summary: Sometimes, when logging out of your NylasID and restarting the app, we would continue making some requests from the worker window that required a NylasID. This would make the app enter a restart loop and become completely unresponsive because when we made a request without a NylasID, we would force the user to log out and restart the app, and then we would again make the requests without the id, ad infinitum. To fix this, we make sure we have a NylasID before making any requests that require it Test Plan: manual Reviewers: halla, evan Reviewed By: halla, evan Differential Revision: https://phab.nylas.com/D4344 --- .../lib/contact-rankings-cache.es6 | 4 +- .../lib/sync-health-checker.es6 | 5 ++- packages/client-app/src/flux/edgehill-api.es6 | 4 ++ .../isomorphic-core/src/metrics-reporter.es6 | 41 ++++++++++--------- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/packages/client-app/internal_packages/contact-rankings/lib/contact-rankings-cache.es6 b/packages/client-app/internal_packages/contact-rankings/lib/contact-rankings-cache.es6 index 3618a2798..bb3c51884 100644 --- a/packages/client-app/internal_packages/contact-rankings/lib/contact-rankings-cache.es6 +++ b/packages/client-app/internal_packages/contact-rankings/lib/contact-rankings-cache.es6 @@ -1,6 +1,7 @@ import _ from 'underscore' import moment from 'moment-timezone' import { + IdentityStore, AccountStore, NylasAPI, NylasAPIRequest, @@ -32,7 +33,8 @@ class ContactRankingsCache extends RefreshingJSONCache { } fetchData = (callback) => { - if (NylasEnv.inSpecMode()) return + if (NylasEnv.inSpecMode()) { return } + if (!IdentityStore.identity()) { return } const request = new NylasAPIRequest({ api: NylasAPI, diff --git a/packages/client-app/internal_packages/sync-health-checker/lib/sync-health-checker.es6 b/packages/client-app/internal_packages/sync-health-checker/lib/sync-health-checker.es6 index 70ba7dbe9..f152232b4 100644 --- a/packages/client-app/internal_packages/sync-health-checker/lib/sync-health-checker.es6 +++ b/packages/client-app/internal_packages/sync-health-checker/lib/sync-health-checker.es6 @@ -1,5 +1,5 @@ import {ipcRenderer} from 'electron' -import {AccountStore, Actions, NylasAPI, NylasAPIRequest} from 'nylas-exports' +import {IdentityStore, AccountStore, Actions, NylasAPI, NylasAPIRequest} from 'nylas-exports' const CHECK_HEALTH_INTERVAL = 5 * 60 * 1000; @@ -35,6 +35,9 @@ class SyncHealthChecker { _checkSyncHealth = async () => { try { + if (!IdentityStore.identity()) { + return + } const request = this._buildRequest() const response = await request.run() this._lastSyncActivity = response diff --git a/packages/client-app/src/flux/edgehill-api.es6 b/packages/client-app/src/flux/edgehill-api.es6 index ff7099fae..3a14ab628 100644 --- a/packages/client-app/src/flux/edgehill-api.es6 +++ b/packages/client-app/src/flux/edgehill-api.es6 @@ -1,4 +1,5 @@ import AccountStore from './stores/account-store' +import IdentityStore from './stores/identity-store' import NylasAPIRequest from './nylas-api-request'; // We're currently moving between services hosted on edgehill-api (written in @@ -34,6 +35,9 @@ class _EdgehillAPI { } if (options.authWithNylasAPI) { + if (!IdentityStore.identity()) { + throw new Error('LegacyEdgehillAPI.makeRequest: Identity must be present to make a request that auths with Nylas API') + } // The account doesn't matter for Edgehill server. We just need to // ensure it's a valid account. options.accountId = AccountStore.accounts()[0].id; diff --git a/packages/isomorphic-core/src/metrics-reporter.es6 b/packages/isomorphic-core/src/metrics-reporter.es6 index 054517852..294ded2a8 100644 --- a/packages/isomorphic-core/src/metrics-reporter.es6 +++ b/packages/isomorphic-core/src/metrics-reporter.es6 @@ -57,27 +57,30 @@ class MetricsReporter { }) try { - if (isClientEnv()) { - if (NylasEnv.inDevMode()) { return } - - if (!dataToReport.accountId) { - throw new Error("Metrics Reporter: You must include an accountId"); - } - - const {N1CloudAPI, NylasAPIRequest} = require('nylas-exports') // eslint-disable-line - const req = new NylasAPIRequest({ - api: N1CloudAPI, - options: { - path: `/ingest-metrics`, - method: 'POST', - body: dataToReport, - accountId: dataToReport.accountId, - }, - }); - await req.run() - } else { + if (!isClientEnv()) { this.sendToHoneycomb(dataToReport) + return } + if (NylasEnv.inDevMode()) { return } + + const {IdentityStore, N1CloudAPI, NylasAPIRequest} = require('nylas-exports') // eslint-disable-line + if (!IdentityStore.identity()) { + throw new Error("Metrics Reporter: Identity must be available"); + } + if (!dataToReport.accountId) { + throw new Error("Metrics Reporter: You must include an accountId"); + } + + const req = new NylasAPIRequest({ + api: N1CloudAPI, + options: { + path: `/ingest-metrics`, + method: 'POST', + body: dataToReport, + accountId: dataToReport.accountId, + }, + }); + await req.run() logger.log("Metrics Reporter: Submitted.", dataToReport); } catch (err) { logger.warn("Metrics Reporter: Submission Failed.", {error: err, ...dataToReport});