This commit is contained in:
Levko Kravets
2017-12-28 18:39:14 +02:00
parent 1bfea8b493
commit fec92b399a
4 changed files with 56 additions and 36 deletions

View File

@@ -1,31 +1,54 @@
import debug from 'debug';
import template from './template.html';
const logger = debug('redash:app-view');
export default function init(ngModule) {
let customHandler = null;
ngModule.factory('$exceptionHandler', () => function exceptionHandler(exception) {
if (customHandler) {
customHandler(exception);
} else {
// eslint-disable-next-line no-console
console.error(exception);
}
});
ngModule.component('appView', {
template,
controller($rootScope, $route, Auth) {
this.showHeaderAndFooter = false;
this.error = null;
customHandler = (error) => {
if (!(error instanceof Error)) {
if (error.status && error.data) {
switch (error.status) {
case 403: error = new Error(''); break;
default: error = new Error(error.data.message); break;
}
}
}
this.error = error;
// eslint-disable-next-line no-console
console.error(error);
};
$rootScope.$on('$routeChangeStart', (event, route) => {
if (route.$$route.authenticated) {
// For routes that need authentication, check if session is already
// loaded, and load it if not.
Auth.logger('Requested authenticated route: ', route);
logger('Requested authenticated route: ', route);
if (Auth.isAuthenticated()) {
this.showHeaderAndFooter = true;
} else {
event.preventDefault();
Auth.loadSession().then(() => {
if (Auth.isAuthenticated()) {
this.showHeaderAndFooter = true;
Auth.logger('Loaded session');
$route.reload();
} else {
throw new Error('Need to login');
}
}).catch(() => {
Auth.logger('Need to login, redirecting');
Auth.login();
// Auth.requireSession resolves only if session loaded
Auth.requireSession().then(() => {
this.showHeaderAndFooter = true;
$route.reload();
});
}
} else {
@@ -33,31 +56,12 @@ export default function init(ngModule) {
}
});
this.error = null;
$rootScope.$on('appViewError', (event, error) => {
if ((error !== null) && (error !== undefined) && (error !== '')) {
this.error = error instanceof Error ? error : new Error('' + error);
} else {
this.error = null;
}
});
$rootScope.$on('$routeChangeSuccess', () => {
$rootScope.$broadcast('appViewError', null);
});
$rootScope.$on('appViewRejection', (event, rejection) => {
let error = null;
switch (rejection.status) {
case 403: error = new Error(''); break;
default: error = new Error(rejection.data.message); break;
}
$rootScope.$broadcast('appViewError', error);
this.error = null;
});
$rootScope.$on('$routeChangeError', (event, current, previous, rejection) => {
$rootScope.$broadcast('appViewRejection', rejection);
throw rejection;
});
},
});

View File

@@ -2,5 +2,5 @@
<div ng-if="$ctrl.error" class="container-fluid">
<div class="alert alert-danger">{{ $ctrl.error.message }}</div>
</div>
<div ng-hide="$ctrl.error" ng-view></div>
<div ng-if="!$ctrl.error" ng-view></div>
<footer ng-if="$ctrl.showHeaderAndFooter"></footer>

View File

@@ -172,7 +172,7 @@ function DashboardCtrl(
this.loadDashboard();
} else {
// all kind of 4** errors are not recoverable, so just display them
$rootScope.$broadcast('appViewRejection', error);
throw error;
}
});
}, 1000);

View File

@@ -24,7 +24,6 @@ function getLocalSessionData() {
function AuthService($window, $location, $q, $http) {
const Auth = {
logger,
isAuthenticated() {
const sessionData = getLocalSessionData();
return sessionData.loaded && sessionData.user.id;
@@ -67,6 +66,23 @@ function AuthService($window, $location, $q, $http) {
getApiKey() {
return this.apiKey;
},
requireSession() {
logger('Requested authentication');
if (Auth.isAuthenticated()) {
return $q.when(getLocalSessionData());
}
return Auth.loadSession().then(() => {
if (Auth.isAuthenticated()) {
logger('Loaded session');
return getLocalSessionData();
}
logger('Need to login, redirecting');
this.login();
}).catch(() => {
logger('Need to login, redirecting');
this.login();
});
},
};
return Auth;