diff --git a/packages/client-app/spec/stores/identity-store-spec.es6 b/packages/client-app/spec/stores/identity-store-spec.es6 index 20c18c8ea..6caccae3f 100644 --- a/packages/client-app/spec/stores/identity-store-spec.es6 +++ b/packages/client-app/spec/stores/identity-store-spec.es6 @@ -76,7 +76,7 @@ describe("IdentityStore", function identityStoreSpec() { const opts = IdentityStore.nylasIDRequest.calls[0].args[0] expect(opts).toEqual({ method: "POST", - url: "https://billing.nylas.com/n1/user/feature_usage_event", + url: "https://billing.nylas.com/api/feature_usage_event", body: { feature_name: 'snooze', }, diff --git a/packages/client-app/src/components/billing-modal.jsx b/packages/client-app/src/components/billing-modal.jsx index d950eac07..e550f1025 100644 --- a/packages/client-app/src/components/billing-modal.jsx +++ b/packages/client-app/src/components/billing-modal.jsx @@ -71,7 +71,7 @@ export default class BillingModal extends React.Component { Actions.closeModal() } }) - await IdentityStore.asyncRefreshIdentity(); + await IdentityStore.fetchIdentity(); }); /** diff --git a/packages/client-app/src/flux/edgehill-api.es6 b/packages/client-app/src/flux/edgehill-api.es6 index 3a14ab628..ef90ea0b6 100644 --- a/packages/client-app/src/flux/edgehill-api.es6 +++ b/packages/client-app/src/flux/edgehill-api.es6 @@ -16,8 +16,6 @@ class _EdgehillAPI { const env = NylasEnv.config.get('env') if (['development', 'local'].includes(env)) { this.APIRoot = "http://n1-auth.lvh.me:5555"; - } else if (env === 'experimental') { - this.APIRoot = "https://edgehill-experimental.nylas.com"; } else if (env === 'staging') { this.APIRoot = "https://n1-auth-staging.nylas.com"; } else { diff --git a/packages/client-app/src/flux/legacy-edgehill-api.es6 b/packages/client-app/src/flux/legacy-edgehill-api.es6 index 831cea6d2..d4f3e4806 100644 --- a/packages/client-app/src/flux/legacy-edgehill-api.es6 +++ b/packages/client-app/src/flux/legacy-edgehill-api.es6 @@ -7,8 +7,6 @@ class LegacyEdgehillAPI extends _EdgehillAPI { const env = NylasEnv.config.get('env') if (['development', 'local'].includes(env)) { this.APIRoot = "http://localhost:5009"; - } else if (env === 'experimental') { - this.APIRoot = "https://edgehill-experimental.nylas.com"; } else if (env === 'staging') { this.APIRoot = "https://edgehill-staging.nylas.com"; } else { diff --git a/packages/client-app/src/flux/models/account.es6 b/packages/client-app/src/flux/models/account.es6 index 855be5714..03dd1ed06 100644 --- a/packages/client-app/src/flux/models/account.es6 +++ b/packages/client-app/src/flux/models/account.es6 @@ -135,17 +135,17 @@ export default class Account extends ModelWithMetadata { } canArchiveThreads() { - CategoryStore = CategoryStore || require('../stores/category-store') + CategoryStore = CategoryStore || require('../stores/category-store').default return CategoryStore.getArchiveCategory(this) } canTrashThreads() { - CategoryStore = CategoryStore || require('../stores/category-store') + CategoryStore = CategoryStore || require('../stores/category-store').default return CategoryStore.getTrashCategory(this) } preferredRemovalDestination() { - CategoryStore = CategoryStore || require('../stores/category-store') + CategoryStore = CategoryStore || require('../stores/category-store').default const preferDelete = NylasEnv.config.get('core.reading.backspaceDelete') if (preferDelete || !CategoryStore.getArchiveCategory(this)) { return CategoryStore.getTrashCategory(this); diff --git a/packages/client-app/src/flux/stores/category-store.es6 b/packages/client-app/src/flux/stores/category-store.es6 index 2c4c32a18..a4bc8dc94 100644 --- a/packages/client-app/src/flux/stores/category-store.es6 +++ b/packages/client-app/src/flux/stores/category-store.es6 @@ -90,7 +90,9 @@ class CategoryStore extends NylasStore { if (!Category.StandardRoles.includes(role)) { throw new Error(`'${role}' is not a standard category`); } - return this._standardCategories[asAccountId(accountOrId)].find(c => c.role === role); + + const accountCategories = this._standardCategories[asAccountId(accountOrId)] + return accountCategories && accountCategories.find(c => c.role === role); } // Public: Returns the set of all standard categories that match the given diff --git a/packages/client-app/src/flux/stores/identity-store.es6 b/packages/client-app/src/flux/stores/identity-store.es6 index af4ec24b3..74fa52bd8 100644 --- a/packages/client-app/src/flux/stores/identity-store.es6 +++ b/packages/client-app/src/flux/stores/identity-store.es6 @@ -65,9 +65,9 @@ class IdentityStore extends NylasStore { * We also update from the server's version every * `SendFeatureUsageEventTask` */ - setInterval(this._fetchIdentity.bind(this), 1000 * 60 * 10); // 10 minutes + setInterval(this.fetchIdentity.bind(this), 1000 * 60 * 10); // 10 minutes // Don't await for this! - this._fetchIdentity(); + this.fetchIdentity(); } /** @@ -79,12 +79,13 @@ class IdentityStore extends NylasStore { if (identity && identity.token) { KeyManager.replacePassword(KEYCHAIN_NAME, identity.token); - delete identity.token; - } - if (!identity) { + const withoutToken = Object.assign({}, identity); + delete withoutToken.token; + NylasEnv.config.set('nylasid', withoutToken); + } else if (!identity) { KeyManager.deletePassword(KEYCHAIN_NAME); + NylasEnv.config.set('nylasid', null); } - NylasEnv.config.set('nylasid', identity); } /** @@ -93,9 +94,7 @@ class IdentityStore extends NylasStore { */ _onIdentityChanged = () => { this._identity = NylasEnv.config.get('nylasid') || {}; - if (!this._identity.token) { - this._identity.token = KeyManager.getPassword(KEYCHAIN_NAME); - } + this._identity.token = KeyManager.getPassword(KEYCHAIN_NAME); this.trigger(); } @@ -110,13 +109,7 @@ class IdentityStore extends NylasStore { _onEnvChanged = () => { const env = NylasEnv.config.get('env') if (['development', 'local'].includes(env)) { - if (process.env.BILLING_URL) { - this.URLRoot = process.env.BILLING_URL; - } else { - this.URLRoot = "https://billing-staging.nylas.com"; - } - } else if (env === 'experimental') { - this.URLRoot = "https://billing-experimental.nylas.com"; + this.URLRoot = "http://localhost:5101"; } else if (env === 'staging') { this.URLRoot = "https://billing-staging.nylas.com"; } else { @@ -156,33 +149,26 @@ class IdentityStore extends NylasStore { body.append(key, qs[key]); } body.append('next_path', pathWithUtm); - body.append('account_token', this._identity.token); - const resp = await fetch(`${this.URLRoot}/n1/login-link`, { - method: 'POST', - qs: qs, - timeout: 1500, - body: body, - }); - if (resp.ok) { - const text = await resp.text(); - if (text.startsWith('http')) { - return text; - } + try { + const json = await this.nylasIDRequest({ + path: '/api/login-link', + qs: qs, + body: body, + timeout: 1500, + method: 'POST', + }); + return `${this.URLRoot}${json.path}`; + } catch (err) { + return `${this.URLRoot}${path}`; } - return `${this.URLRoot}${path}`; } - async asyncRefreshIdentity() { - await this._fetchIdentity(); - return true - } - - async _fetchIdentity() { + async fetchIdentity() { if (!this._identity || !this._identity.token) { return Promise.resolve(); } - const json = await this.fetchPath('/n1/user'); + const json = await this.nylasIDRequest({path: '/api/me', method: 'GET'}); if (!json || !json.id || json.id !== this._identity.id) { console.error(json) NylasEnv.reportError(new Error("Remote Identity returned invalid json"), json || {}) @@ -192,32 +178,25 @@ class IdentityStore extends NylasStore { return this.saveIdentity(nextIdentity); } - fetchPath = async (path) => { - const options = { - method: 'GET', - url: `${this.URLRoot}${path}`, - startTime: Date.now(), - }; + async nylasIDRequest(options) { try { - const newIdentity = await this.nylasIDRequest(options); - return newIdentity + if (options.path) { + options.url = `${this.URLRoot}${options.path}`; + } + options.credentials = 'include'; + options.headers = new Headers(); + options.headers.set('Authorization', `Basic ${btoa(`${this._identity.token}:`)}`) + const resp = await fetch(options.url, options); + if (!resp.ok) { + throw new Error(resp.statusText); + } + return resp.json(); } catch (err) { - const error = err || new Error(`IdentityStore.fetchPath: ${path} ${err.message}.`) + const error = err || new Error(`IdentityStore.nylasIDRequest: ${options.url} ${err.message}.`) NylasEnv.reportError(error) return null } } - - async nylasIDRequest(options) { - options.credentials = 'include'; - options.headers = new Headers(); - options.headers.set('Authorization', `Basic ${btoa(`${this._identity.token}:`)}`) - const resp = await fetch(options.url, options); - if (!resp.ok) { - throw new Error(resp.statusText); - } - return resp.json(); - } } export default new IdentityStore() diff --git a/packages/client-app/src/flux/tasks/send-feature-usage-event-task.es6 b/packages/client-app/src/flux/tasks/send-feature-usage-event-task.es6 index e6f045ec0..7360cfc97 100644 --- a/packages/client-app/src/flux/tasks/send-feature-usage-event-task.es6 +++ b/packages/client-app/src/flux/tasks/send-feature-usage-event-task.es6 @@ -27,7 +27,7 @@ export default class SendFeatureUsageEventTask extends Task { body.append('feature_name', this.featureName); const options = { method: 'POST', - url: `${IdentityStore.URLRoot}/n1/user/feature_usage_event`, + path: `/api/feature_usage_event`, body: body, }; try { diff --git a/packages/client-app/src/package-manager.es6 b/packages/client-app/src/package-manager.es6 index 3f1b3557c..b18ec75ba 100644 --- a/packages/client-app/src/package-manager.es6 +++ b/packages/client-app/src/package-manager.es6 @@ -23,7 +23,7 @@ export default class PackageManager { } } - this.discoverPacakges(); + this.discoverPackages(); } pluginIdFor(packageName) { @@ -31,7 +31,7 @@ export default class PackageManager { return null } - discoverPacakges() { + discoverPackages() { for (const dir of this.packageDirectories) { let filenames = []; try {