diff --git a/bun.lockb b/bun.lockb index ac56349..505d200 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index de55a03..39047c9 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@angular/platform-browser": "^19.0.4", "@angular/platform-browser-dynamic": "^19.0.4", "@angular/router": "^19.0.4", + "keycloak-angular": "^16.1.0", "rxjs": "~7.8.1", "tailwind": "4.0.0", "tslib": "^2.8.1", @@ -38,4 +39,4 @@ "karma-jasmine-html-reporter": "~2.1.0", "typescript": "~5.5.4" } -} \ No newline at end of file +} diff --git a/src/app/app.component.ts b/src/app/app.component.ts index c3fb0ad..fdeeb79 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -3,10 +3,11 @@ import {CommonModule} from '@angular/common'; import {RouterOutlet} from '@angular/router'; @Component({ - selector: 'app-root', + selector: 'app-root', imports: [CommonModule, RouterOutlet], - templateUrl: './app.component.html', - styleUrl: './app.component.css' + templateUrl: './app.component.html', + standalone: true, + styleUrl: './app.component.css' }) export class AppComponent { title = 'lf10StarterNew'; diff --git a/src/app/app.config.ts b/src/app/app.config.ts index bbb5610..15465eb 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,10 +1,50 @@ -import { ApplicationConfig } from '@angular/core'; -import { provideRouter } from '@angular/router'; +import {APP_INITIALIZER, ApplicationConfig} from '@angular/core'; +import {provideRouter} from '@angular/router'; +import {KeycloakAngularModule, KeycloakBearerInterceptor, KeycloakService} from "keycloak-angular"; +import {routes} from './app.routes'; +import {HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi} from "@angular/common/http"; +import {provideAnimationsAsync} from '@angular/platform-browser/animations/async'; -import { routes } from './app.routes'; -import {provideHttpClient, withInterceptorsFromDi} from "@angular/common/http"; -import { provideAnimationsAsync } from '@angular/platform-browser/animations/async'; +export const initializeKeycloak = (keycloak: KeycloakService) => async () => + keycloak.init({ + config: { + url: 'https://keycloak.szut.dev/auth', + realm: 'szut', + clientId: 'employee-management-service-frontend', + }, + loadUserProfileAtStartUp: true, + initOptions: { + onLoad: 'check-sso', + silentCheckSsoRedirectUri: + window.location.origin + '/silent-check-sso.html', + checkLoginIframe: false, + redirectUri: 'http://localhost:4200', + }, + }); + + +function initializeApp(keycloak: KeycloakService): () => Promise { + return () => initializeKeycloak(keycloak)(); +} export const appConfig: ApplicationConfig = { - providers: [provideRouter(routes), provideHttpClient(withInterceptorsFromDi()), provideAnimationsAsync()] + providers: [ + provideRouter(routes), + provideHttpClient(withInterceptorsFromDi()), + provideAnimationsAsync(), + KeycloakAngularModule, + { + provide: APP_INITIALIZER, + useFactory: initializeApp, + multi: true, + deps: [KeycloakService] + }, + KeycloakService, + provideHttpClient(withInterceptorsFromDi()), + { + provide: HTTP_INTERCEPTORS, + useClass: KeycloakBearerInterceptor, + multi: true + } + ] }; diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index c007ee6..5317377 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,7 +1,7 @@ import {Routes} from '@angular/router'; import {EmployeeListComponent} from "./employee-list/employee-list.component"; import {LoginComponent} from "./login/login.component"; -import {AuthGuardService} from "./auth-guard.service"; +import {AuthGuardService} from "./services/auth-guard.service"; export const routes: Routes = [ {path: 'login', component: LoginComponent}, diff --git a/src/app/employee-list/employee-list.component.ts b/src/app/employee-list/employee-list.component.ts index 983cb98..95a3870 100644 --- a/src/app/employee-list/employee-list.component.ts +++ b/src/app/employee-list/employee-list.component.ts @@ -5,10 +5,11 @@ import {HttpClient, HttpHeaders} from "@angular/common/http"; import {Employee} from "../Employee"; @Component({ - selector: 'app-employee-list', - imports: [CommonModule], - templateUrl: './employee-list.component.html', - styleUrl: './employee-list.component.css' + selector: 'app-employee-list', + imports: [CommonModule], + templateUrl: './employee-list.component.html', + standalone: true, + styleUrl: './employee-list.component.css' }) export class EmployeeListComponent { bearer = 'eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6IC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rRVYlW0qFv1EZsE9BmhW3hJwrR1S2IPiEN6MwhehLflLa_ZgLcF417ocIfK-6gbbRNAwXA-JajFVOZAEVXs-52Ta9Kb_EEQFpRsjXorfflmbizQmgrbhBUB7MTiPYIcRruZSYdfmjcE008PHnut52cTcVYEuOrUCUqY4VmhoQ'; diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts index b3f2e24..b32d924 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -4,6 +4,7 @@ import { Component } from '@angular/core'; selector: 'app-login', imports: [], templateUrl: './login.component.html', + standalone: true, styleUrl: './login.component.css' }) export class LoginComponent { diff --git a/src/app/auth-guard.service.ts b/src/app/services/auth-guard.service.ts similarity index 80% rename from src/app/auth-guard.service.ts rename to src/app/services/auth-guard.service.ts index 2d97980..2d65472 100644 --- a/src/app/auth-guard.service.ts +++ b/src/app/services/auth-guard.service.ts @@ -1,6 +1,7 @@ import {Injectable} from '@angular/core'; -import {AuthService} from "./services/auth.service"; +import {AuthService} from "./auth.service"; import {Router} from "@angular/router"; +import {KeycloakService} from "keycloak-angular"; @Injectable({ providedIn: 'root' diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 55b1d26..2a56cef 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -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; } }