Commit eb142f6e authored by Thierry BUGEAT's avatar Thierry BUGEAT

Code refactoring: Load and update tokens when app start.

parent fd451aa6
......@@ -143,55 +143,114 @@
document.webL10n.setLanguage(params.settings.ui.language, "");
}
// Get and set Feedly token from cache then try to update token.
// Get and set Feedly token from cache
// then try to update token
// then try to update subscriptions.
if (params.accounts.feedly.logged) {
my._load('cache/feedly/access_token.json').then(function(_token){
feedly.setToken(_token);
if (navigator.onLine) {
feedly.getSubscriptions();
var _now = Math.floor(new Date().getTime() / 1000);
var _expires_in = _token.expires_in || 604800;
var _tokenIsExpired = ((_now - _token.lastModified) > _expires_in) ? true : false;
if ((!navigator.onLine) && (_tokenIsExpired)) {
_disableAccount('feedly');
}
}).catch(function(error) {
my.alert("Can't load and set Feedly token");
_disableAccount('feedly');
}).then(function(){
if (navigator.onLine) {
my.log("Try to update Feedly token...");
feedly.updateToken();
if (_tokenIsExpired) {
feedly.updateToken().catch(function(error) {
_disableAccount('feedly');
}).then(function(){
if (params.accounts.feedly.logged) {
feedly.getSubscriptions();
}
});
} else {
feedly.getSubscriptions();
}
}
}).catch(function(error) {
my.log("Can't update Feedly token");
_disableAccount('feedly');
my.alert(document.webL10n.get("i-cant-reconnect-your-account", {"online-account": "Feedly"}));
});
}
// Get and set The Old Reader token from cache
// Get and set The Old Reader token from cache
// then try to update token
// then try to update subscriptions.
if (params.accounts.theoldreader.logged) {
my._load('cache/theoldreader/access_token.json').then(function(_token){
theoldreader.setToken(_token);
var _now = Math.floor(new Date().getTime() / 1000);
var _expires_in = _token.expires_in || 604800;
var _tokenIsExpired = ((_now - _token.lastModified) > _expires_in) ? true : false;
if ((!navigator.onLine) && (_tokenIsExpired)) {
_disableAccount('theoldreader');
}
if (navigator.onLine) {
theoldreader.updateToken();
theoldreader.getSubscriptions();
if (_tokenIsExpired) {
theoldreader.updateToken().catch(function(error) {
_disableAccount('theoldreader');
}).then(function(){
if (params.accounts.theoldreader.logged) {
theoldreader.getSubscriptions();
}
});
} else {
theoldreader.getSubscriptions();
}
}
document.getElementById('theoldreaderForm').style.cssText = 'display: none';
}).catch(function(error) {
my.alert(document.webL10n.get("i-cant-reconnect-your-account", {"online-account": "Old Reader"}));
_disableAccount('theoldreader');
my.alert(document.webL10n.get("i-cant-reconnect-your-account", {"online-account": "Old Reader"}));
});
}
// Get and set Aol Reader token from cache
// then try to update token
// then try to update subscriptions.
if (params.accounts.aolreader.logged) {
my._load('cache/aolreader/access_token.json').then(function(_token){
aolreader.setToken(_token);
var _now = Math.floor(new Date().getTime() / 1000);
var _expires_in = _token.expires_in || 604800;
var _tokenIsExpired = ((_now - _token.lastModified) > _expires_in) ? true : false;
if ((!navigator.onLine) && (_tokenIsExpired)) {
_disableAccount('aolreader');
}
if (navigator.onLine) {
aolreader.updateToken();
aolreader.getSubscriptions();
if (_tokenIsExpired) {
aolreader.updateToken().catch(function(error) {
_disableAccount('aolreader');
}).then(function(){
if (params.accounts.aolreader.logged) {
aolreader.getSubscriptions();
}
});
} else {
aolreader.getSubscriptions();
}
}
}).catch(function(error) {
my.alert(document.webL10n.get("i-cant-reconnect-your-account", {"online-account": "Aol Reader"}));
_disableAccount('aolreader');
my.alert(document.webL10n.get("i-cant-reconnect-your-account", {"online-account": "Aol Reader"}));
});
}
......
......@@ -167,25 +167,35 @@ AolReader.prototype.getSubscriptions = function () {
AolReader.prototype.updateToken = function() {
_MyFeeds.log('AolReader.prototype.getNewToken()');
var _url = _AolReader.aolreader.host_auth + '/auth/access_token';
var _params = 'refresh_token=' + encodeURIComponent(_AolReader.aolreader.token.refresh_token) +
'&client_id=' + encodeURIComponent(_AolReader.aolreader.client_id) +
'&client_secret=' + encodeURIComponent(_AolReader.aolreader.client_secret) +
'&grant_type=refresh_token';
this.post(_url, _params, function(response) {
if (typeof response['errorMessage'] !== 'undefined') {
window.alert("AolReader Error : \n" + JSON.stringify(response));
_MyFeeds.log('CustomEvent : AolReader.getNewToken.error');
} else {
_AolReader.aolreader.token.access_token = response.access_token;
_AolReader.aolreader.token.lastModified = Math.floor(new Date().getTime() / 1000);
_AolReader._save('cache/aolreader/access_token.json', 'application/json', JSON.stringify(_AolReader.aolreader.token));
_AolReader._save('cache/aolreader/access_token.new.json', 'application/json', JSON.stringify(response));
document.body.dispatchEvent(new CustomEvent('AolReader.getNewToken.done', {"detail": response}));
_MyFeeds.log('CustomEvent : AolReader.getNewToken.done', response);
}
return new Promise(function(resolve, reject) {
var _url = _AolReader.aolreader.host_auth + '/auth/access_token';
var _params = 'refresh_token=' + encodeURIComponent(_AolReader.aolreader.token.refresh_token) +
'&client_id=' + encodeURIComponent(_AolReader.aolreader.client_id) +
'&client_secret=' + encodeURIComponent(_AolReader.aolreader.client_secret) +
'&grant_type=refresh_token';
var promise = _AolReader.post(_url, _params);
promise.then(function(response) {
if (typeof response['errorMessage'] !== 'undefined') {
window.alert("AolReader Error : \n" + JSON.stringify(response));
_MyFeeds.log('CustomEvent : AolReader.getNewToken.error');
} else {
_AolReader.aolreader.token.access_token = response.access_token;
_AolReader.aolreader.token.lastModified = Math.floor(new Date().getTime() / 1000);
_AolReader._save('cache/aolreader/access_token.json', 'application/json', JSON.stringify(_AolReader.aolreader.token));
_AolReader._save('cache/aolreader/access_token.new.json', 'application/json', JSON.stringify(response));
document.body.dispatchEvent(new CustomEvent('AolReader.getNewToken.done', {"detail": response}));
_MyFeeds.log('CustomEvent : AolReader.getNewToken.done', response);
}
}).catch(function(error) {
document.body.dispatchEvent(new CustomEvent('AolReader.getNewToken.error', {"detail": error}));
_MyFeeds.error("CustomEvent : AolReader.getNewToken.error", error);
reject(Error(JSON.stringify(error)));
});
});
}
......
......@@ -87,25 +87,35 @@ Feedly.prototype.getToken = function() {
Feedly.prototype.updateToken = function() {
_MyFeeds.log('Feedly.prototype.getNewToken()');
var _url = _Feedly.feedly.host + '/v3/auth/token';
var _params = 'refresh_token=' + encodeURIComponent(_Feedly.feedly.token.refresh_token) +
'&client_id=' + encodeURIComponent(_Feedly.feedly.client_id) +
'&client_secret=' + encodeURIComponent(_Feedly.feedly.client_secret) +
'&grant_type=refresh_token';
this.post(_url, _params, function(response) {
if (typeof response['errorMessage'] !== 'undefined') {
window.alert("Feedly Error : \n" + JSON.stringify(response));
_MyFeeds.log('CustomEvent : Feedly.getNewToken.error');
} else {
_Feedly.feedly.token.access_token = response.access_token;
_Feedly.feedly.token.lastModified = Math.floor(new Date().getTime() / 1000);
_Feedly._save('cache/feedly/access_token.json', 'application/json', JSON.stringify(_Feedly.feedly.token));
_Feedly._save('cache/feedly/access_token.new.json', 'application/json', JSON.stringify(response));
document.body.dispatchEvent(new CustomEvent('Feedly.getNewToken.done', {"detail": response}));
_MyFeeds.log('CustomEvent : Feedly.getNewToken.done');
}
return new Promise(function(resolve, reject) {
var _url = _Feedly.feedly.host + '/v3/auth/token';
var _params = 'refresh_token=' + encodeURIComponent(_Feedly.feedly.token.refresh_token) +
'&client_id=' + encodeURIComponent(_Feedly.feedly.client_id) +
'&client_secret=' + encodeURIComponent(_Feedly.feedly.client_secret) +
'&grant_type=refresh_token';
var promise = _Feedly.post(_url, _params);
promise.then(function(response) {
if (typeof response['errorMessage'] !== 'undefined') {
window.alert("Feedly Error : \n" + JSON.stringify(response));
_MyFeeds.log('CustomEvent : Feedly.getNewToken.error');
} else {
_Feedly.feedly.token.access_token = response.access_token;
_Feedly.feedly.token.lastModified = Math.floor(new Date().getTime() / 1000);
_Feedly._save('cache/feedly/access_token.json', 'application/json', JSON.stringify(_Feedly.feedly.token));
_Feedly._save('cache/feedly/access_token.new.json', 'application/json', JSON.stringify(response));
document.body.dispatchEvent(new CustomEvent('Feedly.getNewToken.done', {"detail": response}));
_MyFeeds.log('CustomEvent : Feedly.getNewToken.done');
}
}).catch(function(error) {
document.body.dispatchEvent(new CustomEvent('Feedly.getNewToken.error', {"detail": error}));
_MyFeeds.error("CustomEvent : Feedly.getNewToken.error", error);
reject(Error(JSON.stringify(error)));
});
});
}
......
......@@ -62,6 +62,22 @@ TheOldReader.prototype.getToken = function() {
return this.tor.token;
}
/**
* Use "refresh_token" to obtain a new "access_token"
* @todo Not yet implemented
* @param {null}
* @return {CustomEvent} TheOldReader.getNewToken.done | TheOldReader.getNewToken.error
* */
TheOldReader.prototype.updateToken = function() {
_MyFeeds.log('TheOldReader.prototype.getNewToken()');
return new Promise(function(resolve, reject) {
resolve('{}');
});
}
/**
* @param {string} email
* @param {string} password
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment