This commit is contained in:
Constantin Simonis 2025-03-06 11:01:59 +01:00
commit 33683f565f
No known key found for this signature in database
GPG key ID: 3878FF77C24AF4D2
11 changed files with 330 additions and 294 deletions

View file

@ -0,0 +1,52 @@
import { inject, Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { AuthConfig, OAuthService } from 'angular-oauth2-oidc';
@Injectable({
providedIn: 'root',
})
export class AuthService {
private readonly authConfig: AuthConfig = {
issuer: 'https://oauth.simonis.lol/application/o/casino-dev/',
clientId: 'MDqjm1kcWKuZfqHJXjxwAV20i44aT7m4VhhTL3Nm',
redirectUri: window.location.origin + '/auth/callback',
responseType: 'code',
scope: 'openid profile email',
showDebugInformation: true,
oidc: true,
requestAccessToken: true,
};
private isAuthenticated = new Subject<boolean>();
private oauthService: OAuthService = inject(OAuthService);
constructor() {
this.oauthService.configure(this.authConfig);
this.oauthService.loadDiscoveryDocumentAndTryLogin().then(() => {
this.isAuthenticated.next(this.oauthService.hasValidAccessToken());
});
}
login() {
this.oauthService.initLoginFlow();
}
logout() {
this.oauthService.logOut();
this.isAuthenticated.next(false);
}
getAccessToken() {
return this.oauthService.getAccessToken();
}
getUserInfo() {
return this.oauthService.loadUserProfile();
}
isLoggedIn() {
return this.oauthService.hasValidAccessToken();
}
}

View file

@ -1,6 +1,5 @@
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { KeycloakProfile } from 'keycloak-js';
import { catchError, EMPTY, Observable } from 'rxjs';
import { User } from '../model/User';
@ -20,23 +19,8 @@ export class UserService {
public createUser(id: string, username: string): Observable<User> {
return this.http.post<User>('/backend/user', {
keycloakId: id,
authentikId: id,
username: username,
});
}
public async getOrCreateUser(userProfile: KeycloakProfile) {
if (userProfile.id == null) {
return;
}
return await this.getUser(userProfile.id)
.toPromise()
.then(async (user) => {
if (user) {
return user;
}
return await this.createUser(userProfile.id ?? '', userProfile.username ?? '').toPromise();
});
}
}