Files
start-os/frontend/projects/ui/src/app/guards/auth.guard.ts
Alex Inkin 4f3223d3ad refactor: isolate network toast and login redirect to separate services (#1412)
* refactor: isolate network toast and login redirect to separate services

* chore: remove accidentally committed sketch of a service

* chore: tidying things up

* feat: add `GlobalModule` encapsulating all global subscription services

* remove angular build cache when building deps

* chore: fix more issues found while testing

* chore: fix issues reported by testing

* chore: fix template error

* chore: fix server-info

* chore: fix server-info

* fix: switch to Observable to fix race conditions

* fix embassy name display on load

* update patchdb

* clean up patch data watch

Co-authored-by: Lucy Cifferello <12953208+elvece@users.noreply.github.com>
2022-05-26 16:56:47 -06:00

30 lines
821 B
TypeScript

import { Injectable } from '@angular/core'
import { CanActivate, Router, CanActivateChild, UrlTree } from '@angular/router'
import { map } from 'rxjs/operators'
import { AuthService } from '../services/auth.service'
import { Observable } from 'rxjs'
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(
private readonly authService: AuthService,
private readonly router: Router,
) {}
canActivate(): Observable<boolean | UrlTree> {
return this.runAuthCheck()
}
canActivateChild(): Observable<boolean | UrlTree> {
return this.runAuthCheck()
}
private runAuthCheck(): Observable<boolean | UrlTree> {
return this.authService.isVerified$.pipe(
map(verified => verified || this.router.parseUrl('/login')),
)
}
}