From 3e9fd2e17ffd8263fd8fe9c4444374eb8e3127bd Mon Sep 17 00:00:00 2001 From: oznu Date: Fri, 2 Oct 2020 11:54:32 +1000 Subject: [PATCH] fix #865 --- src/core/config/config.service.ts | 2 +- ui/src/app/app-routing.module.ts | 2 ++ ui/src/app/core/auth/auth.guard.ts | 2 +- ui/src/app/core/auth/auth.module.ts | 2 ++ ui/src/app/core/auth/auth.service.ts | 5 +-- ui/src/app/core/auth/login/login.guard.ts | 31 +++++++++++++++++++ .../settings-plugins-modal.component.ts | 2 +- 7 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 ui/src/app/core/auth/login/login.guard.ts diff --git a/src/core/config/config.service.ts b/src/core/config/config.service.ts index 8cbf333ae..a3ce93f2a 100644 --- a/src/core/config/config.service.ts +++ b/src/core/config/config.service.ts @@ -144,7 +144,7 @@ export class ConfigService { } if (!this.ui.sessionTimeout) { - this.ui.sessionTimeout = 28800; + this.ui.sessionTimeout = this.ui.auth === 'none' ? 1296000 : 28800; } this.secrets = this.getSecrets(); diff --git a/ui/src/app/app-routing.module.ts b/ui/src/app/app-routing.module.ts index 066e6d421..e93ede2f4 100644 --- a/ui/src/app/app-routing.module.ts +++ b/ui/src/app/app-routing.module.ts @@ -4,6 +4,7 @@ import { LayoutComponent } from './shared/layout/layout.component'; import { AuthGuard } from './core/auth/auth.guard'; import { AdminGuard } from './core/auth/admin.guard'; import { LoginComponent } from './core/auth/login/login.component'; +import { LoginGuard } from './core/auth/login/login.guard'; /* * The status and restart modules should not be lazy loaded @@ -16,6 +17,7 @@ const routes: Routes = [ { path: 'login', component: LoginComponent, + canActivate: [LoginGuard], }, { path: '', diff --git a/ui/src/app/core/auth/auth.guard.ts b/ui/src/app/core/auth/auth.guard.ts index 334569b58..3b3295322 100644 --- a/ui/src/app/core/auth/auth.guard.ts +++ b/ui/src/app/core/auth/auth.guard.ts @@ -17,7 +17,7 @@ export class AuthGuard implements CanActivate { state: RouterStateSnapshot): Promise { // ensure app settings are loaded if (!this.$auth.settingsLoaded) { - await this.$auth.getAppSettings(); + await this.$auth.onSettingsLoaded.toPromise(); } if (this.$auth.isLoggedIn()) { diff --git a/ui/src/app/core/auth/auth.module.ts b/ui/src/app/core/auth/auth.module.ts index 3118050cd..8b3c7ba25 100644 --- a/ui/src/app/core/auth/auth.module.ts +++ b/ui/src/app/core/auth/auth.module.ts @@ -9,6 +9,7 @@ import { LoginComponent } from './login/login.component'; import { AuthService } from './auth.service'; import { AuthGuard } from './auth.guard'; import { AdminGuard } from './admin.guard'; +import { LoginGuard } from './login/login.guard'; // token getter export function tokenGetter() { @@ -38,6 +39,7 @@ export function tokenGetter() { AuthService, AuthGuard, AdminGuard, + LoginGuard, ], exports: [], }) diff --git a/ui/src/app/core/auth/auth.service.ts b/ui/src/app/core/auth/auth.service.ts index e90a74ef7..9ea33ff53 100644 --- a/ui/src/app/core/auth/auth.service.ts +++ b/ui/src/app/core/auth/auth.service.ts @@ -134,9 +134,10 @@ export class AuthService { const timeout = expires.diff(dayjs(), 'millisecond'); // setTimeout only accepts a 32bit integer, if the number is larger than this, do not timeout if (timeout <= 2147483647) { - this.logoutTimer = setTimeout(() => { + this.logoutTimer = setTimeout(async () => { if (this.formAuth === false) { - this.noauth(); + await this.noauth(); + window.location.reload(); } else { this.logout(); } diff --git a/ui/src/app/core/auth/login/login.guard.ts b/ui/src/app/core/auth/login/login.guard.ts new file mode 100644 index 000000000..31bf33339 --- /dev/null +++ b/ui/src/app/core/auth/login/login.guard.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@angular/core'; +import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; +import { AuthService } from '@/app/core/auth/auth.service'; + +@Injectable({ + providedIn: 'root', +}) +export class LoginGuard implements CanActivate { + constructor( + private $router: Router, + private $auth: AuthService, + ) { } + + async canActivate( + next: ActivatedRouteSnapshot, + state: RouterStateSnapshot): Promise { + // ensure app settings are loaded + if (!this.$auth.settingsLoaded) { + await this.$auth.onSettingsLoaded.toPromise(); + } + + // if using not using auth, or already logged in, redirect back to home screen + if (this.$auth.formAuth === false || this.$auth.isLoggedIn()) { + // redirect to login page + this.$router.navigate(['/']); + return false; + } + + return true; + } +} diff --git a/ui/src/app/core/manage-plugins/settings-plugins-modal/settings-plugins-modal.component.ts b/ui/src/app/core/manage-plugins/settings-plugins-modal/settings-plugins-modal.component.ts index 03d08e834..8c81cd2b8 100644 --- a/ui/src/app/core/manage-plugins/settings-plugins-modal/settings-plugins-modal.component.ts +++ b/ui/src/app/core/manage-plugins/settings-plugins-modal/settings-plugins-modal.component.ts @@ -203,7 +203,7 @@ export class SettingsPluginsModalComponent implements OnInit { // reload app settings if the config was changed for Homebridge Config UI X if (this.pluginName === 'homebridge-config-ui-x') { - this.$auth.getAppSettings(); + this.$auth.getAppSettings().catch(/* do nothing */); } }) .catch(err => {