Skip to content

Commit

Permalink
fix #865
Browse files Browse the repository at this point in the history
  • Loading branch information
oznu committed Oct 2, 2020
1 parent 9898104 commit 3e9fd2e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/core/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions ui/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +17,7 @@ const routes: Routes = [
{
path: 'login',
component: LoginComponent,
canActivate: [LoginGuard],
},
{
path: '',
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/core/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class AuthGuard implements CanActivate {
state: RouterStateSnapshot): Promise<boolean> {
// ensure app settings are loaded
if (!this.$auth.settingsLoaded) {
await this.$auth.getAppSettings();
await this.$auth.onSettingsLoaded.toPromise();
}

if (this.$auth.isLoggedIn()) {
Expand Down
2 changes: 2 additions & 0 deletions ui/src/app/core/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -38,6 +39,7 @@ export function tokenGetter() {
AuthService,
AuthGuard,
AdminGuard,
LoginGuard,
],
exports: [],
})
Expand Down
5 changes: 3 additions & 2 deletions ui/src/app/core/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
31 changes: 31 additions & 0 deletions ui/src/app/core/auth/login/login.guard.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
// 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 3e9fd2e

Please sign in to comment.