wip
This commit is contained in:
parent
32aa753452
commit
33683f565f
11 changed files with 330 additions and 294 deletions
52
frontend/src/app/service/auth.service.ts
Normal file
52
frontend/src/app/service/auth.service.ts
Normal 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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue