diff --git a/app/routes/application.js b/app/routes/application.js index 0593b58..0a4eaed 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -1,5 +1,36 @@ import Route from '@ember/routing/route'; +import { inject as service } from '@ember/service'; import BodyClassMixin from 'ember-body-class/mixins/body-class'; export default Route.extend(BodyClassMixin, { + + storage: service(), + + beforeModel () { + this.get('storage.rs').on('error', (error) => { + console.debug('rs.on error', error); + if (error.name === 'Unauthorized') { + this.handleUnauthorized(); + } else { + alert('An unknown error occured. Please check the browser console for details.'); + } + }); + }, + + // We need to handle this here, so we can transitionTo + // the connect route + handleUnauthorized () { + // Ignore additional unauthorized events after the + // first one + if (this.get('storage.unauthorized')) { return; } + + this.get('storage').setProperties({ + unauthorized: true, + connecting: false, + connected: false + }); + + this.transitionTo('connect'); + } + }); diff --git a/app/services/storage.js b/app/services/storage.js index d24ba74..efa47ab 100644 --- a/app/services/storage.js +++ b/app/services/storage.js @@ -1,6 +1,7 @@ import EmberObject from '@ember/object'; -import { computed, observer } from '@ember/object'; import Service from '@ember/service'; +import { computed, observer } from '@ember/object'; +import { isEmpty } from '@ember/utils'; import RemoteStorage from 'npm:remotestoragejs'; import Widget from 'npm:remotestorage-widget'; import simpleContentType from 'inspektor/utils/simple-content-type'; @@ -11,6 +12,7 @@ export default Service.extend({ widget: null, connecting: true, connected: false, + unauthorized: false, userAddress: null, disconnected: computed.not('connected'), client: null, @@ -101,6 +103,8 @@ export default Service.extend({ let items = []; return this.get('client').getListing(path).then(listing => { + if (isEmpty(listing)) { return []; } + Object.keys(listing).forEach(name => { let item = listing[name]; let type = item['Content-Type'] || 'folder'; diff --git a/app/templates/connect.hbs b/app/templates/connect.hbs index e2147ca..e69de29 100644 --- a/app/templates/connect.hbs +++ b/app/templates/connect.hbs @@ -1 +0,0 @@ -{{outlet}} \ No newline at end of file