This commit is contained in:
Bernd Heidemann 2024-09-11 10:40:00 +02:00
commit 3b669918d0
59 changed files with 16928 additions and 0 deletions

View file

@ -0,0 +1,6 @@
export class Hello {
constructor(public id?: number,
public message?: string,
) {
}
}

View file

View file

@ -0,0 +1,8 @@
<h1>LF12 Starter</h1>
<a href="/hello">hello</a>
<router-outlet></router-outlet>

View file

@ -0,0 +1,22 @@
import {APP_INITIALIZER, Component} from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import {HelloListComponent} from "./hello-list/hello-list.component";
import {KeycloakAngularModule} from "keycloak-angular";
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, HelloListComponent, RouterOutlet, KeycloakAngularModule],
providers: [ ],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent{
constructor() {
}
}

View file

@ -0,0 +1,48 @@
import {APP_INITIALIZER, ApplicationConfig} from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
import {KeycloakAngularModule, KeycloakBearerInterceptor, KeycloakService} from "keycloak-angular";
import {HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi} from "@angular/common/http";
export const initializeKeycloak = (keycloak: KeycloakService) => async () =>
keycloak.init({
config: {
url: 'http://localhost:9090',
realm: 'LF12',
clientId: 'lf12',
},
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<boolean> {
return () => initializeKeycloak(keycloak)();
}
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes),
KeycloakAngularModule,
{
provide: APP_INITIALIZER,
useFactory: initializeApp,
multi: true,
deps: [KeycloakService]
},
KeycloakService,
provideHttpClient(withInterceptorsFromDi()),
{
provide: HTTP_INTERCEPTORS,
useClass: KeycloakBearerInterceptor,
multi: true
}
]
};

View file

@ -0,0 +1,7 @@
import { Routes } from '@angular/router';
import {HelloListComponent} from "./hello-list/hello-list.component";
import {authGuard} from "./auth.guard";
export const routes: Routes = [
{ path: 'hello', component: HelloListComponent , canActivate: [authGuard] }
];

View file

@ -0,0 +1,17 @@
import { CanActivateFn } from '@angular/router';
import { inject } from '@angular/core';
import {KeycloakService} from "keycloak-angular";
export const authGuard: CanActivateFn = async (route, state) => {
const keycloakService = inject(KeycloakService);
const isLoggedIn = keycloakService.isLoggedIn();
if (isLoggedIn) {
return true;
} else {
keycloakService.login();
return false;
}
};

View file

@ -0,0 +1,8 @@
<h3>List of Hellos</h3>
<ul>
@for(e of employees$ | async; track e.id) {
<li>
{{e.id }}, {{e.message}}
</li>
}
</ul>

View file

@ -0,0 +1,31 @@
import { Component } from '@angular/core';
import {Observable, of} from "rxjs";
import {Hello} from "../Hello";
import {HttpClient, HttpClientModule, HttpHeaders, provideHttpClient} from "@angular/common/http";
import {AsyncPipe} from "@angular/common";
import {KeycloakService} from "keycloak-angular";
@Component({
selector: 'app-hello-list',
standalone: true,
imports: [
AsyncPipe
],
providers: [KeycloakService],
templateUrl: './hello-list.component.html',
styleUrl: './hello-list.component.css'
})
export class HelloListComponent {
employees$: Observable<Hello[]>;
constructor(private http: HttpClient) {
this.employees$ = of([]);
this.fetchData();
}
fetchData() {
this.employees$ = this.http.get<Hello[]>('/backend/hellos');
}
}