Configure keycloak (#4)
Co-authored-by: Phan Huy Tran <p.tran@neusta.de> Reviewed-on: #4 Co-authored-by: ptran <huydw@proton.me> Co-committed-by: ptran <huydw@proton.me>
This commit is contained in:
21
src/app/services/auth-guard.service.ts
Normal file
21
src/app/services/auth-guard.service.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {AuthService} from "./auth.service";
|
||||
import {Router} from "@angular/router";
|
||||
import {KeycloakService} from "keycloak-angular";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthGuardService {
|
||||
constructor(public auth: AuthService, public router: Router) {
|
||||
}
|
||||
|
||||
canActivate(): boolean {
|
||||
if (!this.auth.isAuthenticated()) {
|
||||
this.router.navigate(['login']);
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,10 +1,19 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {KeycloakService} from "keycloak-angular";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthService {
|
||||
private keycloakService = inject(KeycloakService);
|
||||
|
||||
public isAuthenticated(): boolean {
|
||||
if (this.keycloakService.isLoggedIn()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
this.keycloakService.login();
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user