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:
ptran
2024-12-18 10:02:18 +00:00
committed by Constantin Simonis
parent 4e27c3fb2e
commit 934716fb3e
9 changed files with 71 additions and 17 deletions

View 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;
}
}

View File

@ -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;
}
}