We do have global rest error handler which take care of re-login
This commit is contained in:
@@ -23,7 +23,7 @@ angular.module('Application').service('Client', ['$http', 'md5', 'Notification',
|
||||
|
||||
function defaultErrorHandler(callback) {
|
||||
return function (data, status) {
|
||||
if (status === 401) return client.logout();
|
||||
if (status === 401) return client.login();
|
||||
if (status === 503) {
|
||||
// this could indicate a update/upgrade/restore/migration
|
||||
client.progress(function (error, result) {
|
||||
@@ -605,12 +605,27 @@ angular.module('Application').service('Client', ['$http', 'md5', 'Notification',
|
||||
});
|
||||
};
|
||||
|
||||
Client.prototype.login = function () {
|
||||
this.setToken(null);
|
||||
this._userInfo = {};
|
||||
|
||||
var callbackURL = window.location.protocol + '//' + window.location.host + '/login_callback.html';
|
||||
var scope = 'root,profile,apps,roleAdmin';
|
||||
|
||||
// generate a state id to protect agains csrf
|
||||
var state = Math.floor((1 + Math.random()) * 0x1000000000000).toString(16).substring(1);
|
||||
window.localStorage.oauth2State = state;
|
||||
|
||||
window.location.href = this.apiOrigin + '/api/v1/oauth/dialog/authorize?response_type=token&client_id=' + this._clientId + '&redirect_uri=' + callbackURL + '&scope=' + scope + '&state=' + state;
|
||||
};
|
||||
|
||||
Client.prototype.logout = function () {
|
||||
this.setToken(null);
|
||||
this._userInfo = {};
|
||||
|
||||
// logout from OAuth session
|
||||
window.location.href = client.apiOrigin + '/api/v1/session/logout';
|
||||
var origin = window.location.protocol + "//" + window.location.host;
|
||||
window.location.href = this.apiOrigin + '/api/v1/session/logout?redirect=' + origin;
|
||||
};
|
||||
|
||||
Client.prototype.exchangeCodeForToken = function (code, callback) {
|
||||
|
||||
@@ -23,17 +23,6 @@ angular.module('Application').controller('MainController', ['$scope', '$route',
|
||||
Client.logout();
|
||||
};
|
||||
|
||||
$scope.login = function () {
|
||||
var callbackURL = window.location.protocol + '//' + window.location.host + '/login_callback.html';
|
||||
var scope = 'root,profile,apps,roleAdmin';
|
||||
|
||||
// generate a state id to protect agains csrf
|
||||
var state = Math.floor((1 + Math.random()) * 0x1000000000000).toString(16).substring(1);
|
||||
window.localStorage.oauth2State = state;
|
||||
|
||||
window.location.href = Client.apiOrigin + '/api/v1/oauth/dialog/authorize?response_type=token&client_id=' + Client._clientId + '&redirect_uri=' + callbackURL + '&scope=' + scope + '&state=' + state;
|
||||
};
|
||||
|
||||
$scope.setup = function () {
|
||||
window.location.href = '/error.html?errorCode=1';
|
||||
};
|
||||
@@ -78,51 +67,43 @@ angular.module('Application').controller('MainController', ['$scope', '$route',
|
||||
if (error) return $scope.error(error);
|
||||
if (isFirstTime) return $scope.setup();
|
||||
|
||||
// we use the config request as an indicator if the token is still valid
|
||||
// TODO we should probably attach such a handler for each request, as the token can get invalid
|
||||
// at any time!
|
||||
if (localStorage.token) {
|
||||
Client.refreshConfig(function (error) {
|
||||
if (error && error.statusCode === 401) return $scope.login();
|
||||
Client.refreshConfig(function (error) {
|
||||
if (error) return $scope.error(error);
|
||||
|
||||
// check version and force reload if needed
|
||||
if (!localStorage.version) {
|
||||
localStorage.version = Client.getConfig().version;
|
||||
} else if (localStorage.version !== Client.getConfig().version) {
|
||||
localStorage.version = Client.getConfig().version;
|
||||
window.location.reload(true);
|
||||
}
|
||||
|
||||
Client.refreshUserInfo(function (error, result) {
|
||||
if (error) return $scope.error(error);
|
||||
|
||||
// check version and force reload if needed
|
||||
if (!localStorage.version) {
|
||||
localStorage.version = Client.getConfig().version;
|
||||
} else if (localStorage.version !== Client.getConfig().version) {
|
||||
localStorage.version = Client.getConfig().version;
|
||||
window.location.reload(true);
|
||||
}
|
||||
|
||||
Client.refreshUserInfo(function (error, result) {
|
||||
Client.refreshInstalledApps(function (error) {
|
||||
if (error) return $scope.error(error);
|
||||
|
||||
Client.refreshInstalledApps(function (error) {
|
||||
if (error) return $scope.error(error);
|
||||
// kick off installed apps and config polling
|
||||
var refreshAppsTimer = $interval(Client.refreshInstalledApps.bind(Client), 2000);
|
||||
var refreshConfigTimer = $interval(Client.refreshConfig.bind(Client), 5000);
|
||||
var refreshUserInfoTimer = $interval(Client.refreshUserInfo.bind(Client), 5000);
|
||||
|
||||
// kick off installed apps and config polling
|
||||
var refreshAppsTimer = $interval(Client.refreshInstalledApps.bind(Client), 2000);
|
||||
var refreshConfigTimer = $interval(Client.refreshConfig.bind(Client), 5000);
|
||||
var refreshUserInfoTimer = $interval(Client.refreshUserInfo.bind(Client), 5000);
|
||||
|
||||
$scope.$on('$destroy', function () {
|
||||
$interval.cancel(refreshAppsTimer);
|
||||
$interval.cancel(refreshConfigTimer);
|
||||
$interval.cancel(refreshUserInfoTimer);
|
||||
});
|
||||
|
||||
// now mark the Client to be ready
|
||||
Client.setReady();
|
||||
|
||||
$scope.config = Client.getConfig();
|
||||
|
||||
$scope.initialized = true;
|
||||
$scope.$on('$destroy', function () {
|
||||
$interval.cancel(refreshAppsTimer);
|
||||
$interval.cancel(refreshConfigTimer);
|
||||
$interval.cancel(refreshUserInfoTimer);
|
||||
});
|
||||
|
||||
// now mark the Client to be ready
|
||||
Client.setReady();
|
||||
|
||||
$scope.config = Client.getConfig();
|
||||
|
||||
$scope.initialized = true;
|
||||
});
|
||||
});
|
||||
} else {
|
||||
$scope.login();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// wait till the view has loaded until showing a modal dialog
|
||||
|
||||
Reference in New Issue
Block a user