diff --git a/app/ux/Auth0.js b/app/ux/Auth0.js index ef2216cf..764ac955 100644 --- a/app/ux/Auth0.js +++ b/app/ux/Auth0.js @@ -13,7 +13,8 @@ Ext.define('Rambox.ux.Auth0', { autoclose: true ,autofocus: true ,auth: { - redirect: false + redirect: false + ,params: {scope: 'openid offline_access'} } ,theme: { logo: 'resources/Icon.png' @@ -54,6 +55,7 @@ Ext.define('Rambox.ux.Auth0', { // Save the profile and JWT. localStorage.setItem('profile', JSON.stringify(profile)); localStorage.setItem('id_token', authResult.idToken); + localStorage.setItem('refresh_token', authResult.refreshToken); if ( !Ext.isEmpty(profile.user_metadata) && !Ext.isEmpty(profile.user_metadata.services) ) { Ext.each(profile.user_metadata.services, function(s) { @@ -81,9 +83,10 @@ Ext.define('Rambox.ux.Auth0', { var lastupdate = (new Date()).toJSON(); var services = []; Ext.getStore('Services').each(function(service) { - delete service.data.id; - delete service.data.zoomLevel; - services.push(service.data); + var s = Ext.clone(service); + delete s.data.id; + delete s.data.zoomLevel; + services.push(s.data); }); Ext.Ajax.request({ @@ -95,6 +98,7 @@ Ext.define('Rambox.ux.Auth0', { Ext.Msg.hide(); // Save the last update in localStorage var profile = Ext.decode(localStorage.getItem('profile')); + if ( !profile.user_metadata ) profile.user_metadata = {}; profile.user_metadata.services_lastupdate = lastupdate; localStorage.setItem('profile', Ext.encode(profile)); Ext.cq1('app-main').getViewModel().set('last_sync', new Date(lastupdate).toUTCString()); @@ -108,6 +112,8 @@ Ext.define('Rambox.ux.Auth0', { }); } ,failure: function(response) { + if ( response.status === 401 ) return me.renewToken(me.backupConfiguration); + Ext.Msg.hide(); Ext.toast({ html: ' Error occurred when trying to backup your configuration.' @@ -124,10 +130,14 @@ Ext.define('Rambox.ux.Auth0', { ,restoreConfiguration: function() { var me = this; - Ext.cq1('app-main').getController().removeAllServices(false, function() { - me.lock.getProfile(localStorage.getItem('id_token'), function (err, profile) { - if (err) return alert('There was an error getting the profile: ' + err.message); + me.lock.getProfile(localStorage.getItem('id_token'), function (err, profile) { + if ( err ) { + if ( err.error === 401 ) return me.renewToken(me.restoreConfiguration); + return alert('There was an error getting the profile: ' + err.message); + } + // First we remove all current services + Ext.cq1('app-main').getController().removeAllServices(false, function() { Ext.each(profile.user_metadata.services, function(s) { var service = Ext.create('Rambox.model.Service', s); service.save(); @@ -143,7 +153,21 @@ Ext.define('Rambox.ux.Auth0', { var me = this; me.lock.getProfile(localStorage.getItem('id_token'), function (err, profile) { - if (err) return alert('There was an error getting the profile: ' + err.message); + if ( err ) { + if ( err.error === 401 ) return me.renewToken(me.checkConfiguration); + return alert('There was an error getting the profile: ' + err.message); + } + + if ( !profile.user_metadata ) { + Ext.toast({ + html: 'You don\'t have any backup yet.' + ,title: 'Synchronize Configuration' + ,width: 300 + ,align: 't' + ,closable: false + }); + return; + } if ( Math.floor(new Date(profile.user_metadata.services_lastupdate) / 1000) > Math.floor(new Date(Ext.decode(localStorage.getItem('profile')).user_metadata.services_lastupdate) / 1000) ) { Ext.toast({ @@ -165,6 +189,30 @@ Ext.define('Rambox.ux.Auth0', { }); } + ,renewToken: function(callback) { + var me = this; + + Ext.Ajax.request({ + url: 'https://rambox.auth0.com/delegation' + ,method: 'POST' + ,jsonData: { + grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer' + ,client_id: auth0Cfg.clientID + ,refresh_token: localStorage.getItem('refresh_token') + ,api_type: 'app' + } + ,success: function(response) { + var json = Ext.decode(response.responseText); + localStorage.setItem('id_token', json.id_token); + + if ( Ext.isFunction(callback) ) callback.bind(me)(); + } + ,failure: function(response) { + console.error(response); + } + }); + } + ,login: function() { var me = this; @@ -176,5 +224,6 @@ Ext.define('Rambox.ux.Auth0', { localStorage.removeItem('profile'); localStorage.removeItem('id_token'); + localStorage.removeItem('refresh_token'); } }); diff --git a/app/view/main/MainController.js b/app/view/main/MainController.js index 786ae565..024a9067 100644 --- a/app/view/main/MainController.js +++ b/app/view/main/MainController.js @@ -102,10 +102,10 @@ Ext.define('Rambox.view.main.MainController', { Ext.Msg.confirm('Please confirm...', 'Are you sure you want to remove all services?', function(btnId) { if ( btnId === 'yes' ) { Ext.cq1('app-main').suspendEvent('remove'); + Ext.getStore('Services').load(); Ext.Array.each(Ext.getStore('Services').collect('id'), function(serviceId) { me.removeServiceFn(serviceId); }); - Ext.getStore('Services').load(); if ( Ext.isFunction(callback) ) callback(); Ext.cq1('app-main').resumeEvent('remove'); document.title = 'Rambox'; @@ -113,10 +113,10 @@ Ext.define('Rambox.view.main.MainController', { }); } else { Ext.cq1('app-main').suspendEvent('remove'); + Ext.getStore('Services').load(); Ext.Array.each(Ext.getStore('Services').collect('id'), function(serviceId) { me.removeServiceFn(serviceId); }); - Ext.getStore('Services').load(); if ( Ext.isFunction(callback) ) callback(); Ext.cq1('app-main').resumeEvent('remove'); document.title = 'Rambox';