frontend skeleton
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { effect, inject, Injectable, signal } from '@angular/core';
|
||||
import {
|
||||
KEYCLOAK_EVENT_SIGNAL,
|
||||
KeycloakEventType,
|
||||
ReadyArgs,
|
||||
typeEventArgs,
|
||||
} from 'keycloak-angular';
|
||||
import Keycloak, { KeycloakProfile } from 'keycloak-js';
|
||||
|
||||
export enum AuthenticationState {
|
||||
Authenticated,
|
||||
Unauthenticated,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class Authentication {
|
||||
private readonly keycloak = inject(Keycloak);
|
||||
private readonly keycloakSignal = inject(KEYCLOAK_EVENT_SIGNAL);
|
||||
private readonly _authenticationState = signal<AuthenticationState>(AuthenticationState.Unknown);
|
||||
private readonly _userInfo = signal<KeycloakProfile | null>(null);
|
||||
public readonly authenticationState = this._authenticationState.asReadonly();
|
||||
public readonly userInfo = this._userInfo.asReadonly();
|
||||
|
||||
public constructor() {
|
||||
effect(() => {
|
||||
const event = this.keycloakSignal();
|
||||
switch (event?.type) {
|
||||
case KeycloakEventType.Ready:
|
||||
this._authenticationState.set(
|
||||
typeEventArgs<ReadyArgs>(event.args)
|
||||
? AuthenticationState.Authenticated
|
||||
: AuthenticationState.Unauthenticated
|
||||
);
|
||||
break;
|
||||
case KeycloakEventType.AuthSuccess:
|
||||
this._authenticationState.set(AuthenticationState.Authenticated);
|
||||
break;
|
||||
case KeycloakEventType.AuthLogout:
|
||||
this._authenticationState.set(AuthenticationState.Unauthenticated);
|
||||
break;
|
||||
case KeycloakEventType.AuthError:
|
||||
this._authenticationState.set(AuthenticationState.Unauthenticated);
|
||||
break;
|
||||
case KeycloakEventType.AuthRefreshError:
|
||||
this._authenticationState.set(AuthenticationState.Unauthenticated);
|
||||
break;
|
||||
case KeycloakEventType.AuthRefreshSuccess:
|
||||
this._authenticationState.set(AuthenticationState.Authenticated);
|
||||
break;
|
||||
case KeycloakEventType.TokenExpired:
|
||||
this._authenticationState.set(AuthenticationState.Unauthenticated);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
effect(async () => {
|
||||
const authenticationState = this.authenticationState();
|
||||
if (authenticationState !== AuthenticationState.Authenticated) {
|
||||
this._userInfo.set(null);
|
||||
}
|
||||
const profile = await this.keycloak.loadUserProfile();
|
||||
this._userInfo.set(profile);
|
||||
});
|
||||
}
|
||||
|
||||
public async login(location?: string): Promise<void> {
|
||||
var redirectUri = location
|
||||
? `${window.location.origin}/${location}`
|
||||
: `${window.location.origin}/login`;
|
||||
return await this.keycloak.login({
|
||||
redirectUri: redirectUri,
|
||||
locale: 'de-DE',
|
||||
});
|
||||
}
|
||||
|
||||
public async logout(): Promise<void> {
|
||||
return await this.keycloak.logout({ redirectUri: window.location.origin + '/login' });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user