use guards for the router rather than checking for each state

This commit is contained in:
Jacob Overgaard
2022-06-29 13:16:12 +02:00
parent 368ffb5208
commit e098a3e7a8

View File

@@ -10,6 +10,7 @@ import { UmbContextProviderMixin } from './core/context';
import { UmbExtensionManifest, UmbExtensionManifestCore, UmbExtensionRegistry } from './core/extension';
import { ServerStatus } from './core/models';
import { internalManifests } from './temp-internal-manifests';
import { IRoute } from 'router-slot/model';
@customElement('umb-app')
export class UmbApp extends UmbContextProviderMixin(LitElement) {
@@ -23,7 +24,7 @@ export class UmbApp extends UmbContextProviderMixin(LitElement) {
`;
@state()
private _routes = [
private _routes: IRoute[] = [
{
path: 'login',
component: () => import('./auth/login/login.element'),
@@ -35,10 +36,12 @@ export class UmbApp extends UmbContextProviderMixin(LitElement) {
{
path: 'upgrade',
component: () => import('./upgrader/upgrader.element'),
guards: [this._isAuthorizedGuard.bind(this)],
},
{
path: '**',
component: () => import('./backoffice/backoffice.element'),
guards: [this._isAuthorizedGuard.bind(this)],
},
];
@@ -77,20 +80,17 @@ export class UmbApp extends UmbContextProviderMixin(LitElement) {
break;
case 'must-upgrade':
if (this._isAuthorized()) {
history.replaceState(null, '', '/upgrade');
} else {
history.replaceState(null, '', '/login');
}
history.replaceState(null, '', '/upgrade');
break;
case 'running':
if (this._isAuthorized()) {
history.replaceState(null, '', window.location.pathname);
} else {
history.replaceState(null, '', '/login');
}
case 'running': {
const pathname =
window.location.pathname === '/install' || window.location.pathname === '/upgrade'
? '/'
: window.location.pathname;
history.replaceState(null, '', pathname);
break;
}
}
}
@@ -98,6 +98,15 @@ export class UmbApp extends UmbContextProviderMixin(LitElement) {
return sessionStorage.getItem('is-authenticated') === 'true';
}
private _isAuthorizedGuard(): boolean {
if (this._isAuthorized()) {
return true;
}
history.replaceState(null, '', '/login');
return false;
}
private async _registerExtensionManifestsFromServer() {
// TODO: add schema and use fetcher
const res = await fetch('/umbraco/backoffice/manifests');