mirror of
https://github.com/dkmstr/openuds-gui.git
synced 2025-01-18 10:03:33 +03:00
Added auth guard
This commit is contained in:
parent
f9739b0824
commit
0f3adde78b
@ -1,5 +1,6 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { Routes, RouterModule } from '@angular/router';
|
||||
import { AuthGuard } from './auth.guard';
|
||||
|
||||
import { LoginComponent } from '../pages/login/login.component';
|
||||
import { ClientDownloadComponent } from '../pages/client-download/client-download.component';
|
||||
@ -10,13 +11,13 @@ import { AboutComponent } from '../pages/about/about.component';
|
||||
|
||||
const routes: Routes = [
|
||||
{ path: '', redirectTo: 'services', pathMatch: 'full' },
|
||||
{ path: 'services', component: ServicesComponent },
|
||||
{ path: 'services', component: ServicesComponent, canActivate: [AuthGuard] },
|
||||
|
||||
{ path: 'login', component: LoginComponent },
|
||||
{ path: 'login/:id', component: LoginComponent },
|
||||
|
||||
{ path: 'client-download', component: ClientDownloadComponent },
|
||||
{ path: 'downloads', component: DownloadsComponent },
|
||||
{ path: 'downloads', component: DownloadsComponent, canActivate: [AuthGuard] },
|
||||
|
||||
{ path: 'error/:id', component: ErrorComponent },
|
||||
{ path: 'about', component: AboutComponent },
|
||||
|
24
src/app/modules/auth.guard.ts
Normal file
24
src/app/modules/auth.guard.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||
import { UDSApiService } from '../uds-api.service';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(private api: UDSApiService) {
|
||||
}
|
||||
|
||||
canActivate(
|
||||
next: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
|
||||
// Redirect, if not logged in, to login screen
|
||||
if (!this.api.user.isLogged) {
|
||||
this.api.router.navigate(['login']);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -83,11 +83,6 @@ export class ServicesComponent implements OnInit {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
// Redirect, if not logged in, to login screen
|
||||
if (!this.api.user.isLogged) {
|
||||
this.api.router.navigate(['login']);
|
||||
}
|
||||
|
||||
this.loadServices(); // Loads service related data
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user