mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 12:11:56 +00:00
0.2.5 initial commit
Makefile incomplete
This commit is contained in:
36
ui/src/app/guards/auth.guard.ts
Normal file
36
ui/src/app/guards/auth.guard.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Injectable } from '@angular/core'
|
||||
import { CanActivate, Router, CanActivateChild } from '@angular/router'
|
||||
import { AuthState, AuthService } from '../services/auth.service'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthGuard implements CanActivate, CanActivateChild {
|
||||
constructor (
|
||||
private readonly authService: AuthService,
|
||||
private readonly router: Router,
|
||||
) { }
|
||||
|
||||
canActivate (): boolean {
|
||||
return this.runCheck()
|
||||
}
|
||||
|
||||
canActivateChild (): boolean {
|
||||
return this.runCheck()
|
||||
}
|
||||
|
||||
private runCheck (): boolean {
|
||||
const state = this.authService.peek()
|
||||
|
||||
switch (state){
|
||||
case AuthState.VERIFIED: return true
|
||||
case AuthState.UNVERIFIED: return this.toAuthenticate()
|
||||
case AuthState.INITIALIZING: return this.toAuthenticate()
|
||||
}
|
||||
}
|
||||
|
||||
private toAuthenticate () {
|
||||
this.router.navigate(['/authenticate'], { replaceUrl: true })
|
||||
return false
|
||||
}
|
||||
}
|
||||
26
ui/src/app/guards/deactivate.guard.ts
Normal file
26
ui/src/app/guards/deactivate.guard.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Injectable, Directive } from '@angular/core'
|
||||
import { CanDeactivate } from '@angular/router'
|
||||
import { HostListener } from '@angular/core'
|
||||
|
||||
@Directive()
|
||||
export abstract class PageCanDeactivate {
|
||||
abstract canDeactivate (): boolean
|
||||
|
||||
@HostListener('window:beforeunload', ['$event'])
|
||||
unloadNotification (e: any) {
|
||||
console.log(e)
|
||||
if (!this.canDeactivate()) {
|
||||
e.returnValue = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class CanDeactivateGuard implements CanDeactivate<PageCanDeactivate> {
|
||||
|
||||
canDeactivate (page: PageCanDeactivate): boolean {
|
||||
return page.canDeactivate() || confirm('You have unsaved changes. Are you sure you want to leave the page?')
|
||||
}
|
||||
}
|
||||
26
ui/src/app/guards/unauth.guard.ts
Normal file
26
ui/src/app/guards/unauth.guard.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@angular/core'
|
||||
import { CanActivate, Router } from '@angular/router'
|
||||
import { AuthService, AuthState } from '../services/auth.service'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class UnauthGuard implements CanActivate {
|
||||
constructor (
|
||||
private readonly authService: AuthService,
|
||||
private readonly router: Router,
|
||||
) { }
|
||||
|
||||
canActivate (): boolean {
|
||||
const state = this.authService.peek()
|
||||
switch (state){
|
||||
case AuthState.VERIFIED: {
|
||||
this.router.navigateByUrl('')
|
||||
return false
|
||||
}
|
||||
case AuthState.UNVERIFIED: return true
|
||||
case AuthState.INITIALIZING: return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user