mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-02 05:23:14 +00:00
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>
This commit is contained in:
@@ -1,38 +1,29 @@
|
||||
import { Injectable } from '@angular/core'
|
||||
import { CanActivate, Router, CanActivateChild } from '@angular/router'
|
||||
import { tap } from 'rxjs/operators'
|
||||
import { AuthState, AuthService } from '../services/auth.service'
|
||||
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 {
|
||||
authState: AuthState
|
||||
|
||||
constructor (
|
||||
constructor(
|
||||
private readonly authService: AuthService,
|
||||
private readonly router: Router,
|
||||
) {
|
||||
this.authService.watch$()
|
||||
.pipe(
|
||||
tap(auth => this.authState = auth),
|
||||
).subscribe()
|
||||
}
|
||||
) {}
|
||||
|
||||
canActivate (): boolean {
|
||||
canActivate(): Observable<boolean | UrlTree> {
|
||||
return this.runAuthCheck()
|
||||
}
|
||||
|
||||
canActivateChild (): boolean {
|
||||
canActivateChild(): Observable<boolean | UrlTree> {
|
||||
return this.runAuthCheck()
|
||||
}
|
||||
|
||||
private runAuthCheck (): boolean {
|
||||
if (this.authState === AuthState.VERIFIED) {
|
||||
return true
|
||||
} else {
|
||||
this.router.navigate(['/login'], { replaceUrl: true })
|
||||
return false
|
||||
}
|
||||
private runAuthCheck(): Observable<boolean | UrlTree> {
|
||||
return this.authService.isVerified$.pipe(
|
||||
map(verified => verified || this.router.parseUrl('/login')),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,21 @@
|
||||
import { Injectable } from '@angular/core'
|
||||
import { CanActivate, Router } from '@angular/router'
|
||||
import { tap } from 'rxjs/operators'
|
||||
import { AuthService, AuthState } from '../services/auth.service'
|
||||
import { CanActivate, Router, UrlTree } from '@angular/router'
|
||||
import { map } from 'rxjs/operators'
|
||||
import { AuthService } from '../services/auth.service'
|
||||
import { Observable } from 'rxjs'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class UnauthGuard implements CanActivate {
|
||||
authState: AuthState
|
||||
|
||||
constructor (
|
||||
constructor(
|
||||
private readonly authService: AuthService,
|
||||
private readonly router: Router,
|
||||
) {
|
||||
this.authService.watch$()
|
||||
.pipe(
|
||||
tap(auth => this.authState = auth),
|
||||
).subscribe()
|
||||
}
|
||||
) {}
|
||||
|
||||
canActivate (): boolean {
|
||||
if (this.authState === AuthState.VERIFIED) {
|
||||
this.router.navigateByUrl('')
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
canActivate(): Observable<boolean | UrlTree> {
|
||||
return this.authService.isVerified$.pipe(
|
||||
map(verified => !verified || this.router.parseUrl('')),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user