This commit is contained in:
Constantin Simonis 2025-03-06 12:52:15 +01:00
commit f547d05f64
No known key found for this signature in database
GPG key ID: 3878FF77C24AF4D2
8 changed files with 78 additions and 104 deletions

View file

@ -1,29 +1,47 @@
import { inject, Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { AuthConfig, OAuthService } from 'angular-oauth2-oidc';
import { UserService } from './user.service';
import { User } from '../model/User';
@Injectable({
providedIn: 'root',
})
export class AuthService {
private userService: UserService = inject(UserService);
private readonly authConfig: AuthConfig = {
issuer: 'https://oauth.simonis.lol/application/o/casino-dev/',
loginUrl: 'https://oauth.simonis.lol/application/o/authorize/',
tokenEndpoint: 'https://oauth.simonis.lol/application/o/token/',
userinfoEndpoint: 'https://oauth.simonis.lol/application/o/userinfo/',
clientId: 'MDqjm1kcWKuZfqHJXjxwAV20i44aT7m4VhhTL3Nm',
redirectUri: window.location.origin + '/auth/callback',
responseType: 'code',
dummyClientSecret: 'GY2F8te6iAVYt1TNAUVLzWZEXb6JoMNp6chbjqaXNq4gS5xTDL54HqBiAlV1jFKarN28LQ7FUsYX4SbwjfEhZhgeoKuBnZKjR9eiu7RawnGgxIK9ffvUfMkjRxnmiGI5',
scope: 'openid profile email',
showDebugInformation: true,
responseType: 'code',
redirectUri: window.location.origin + '/auth/callback',
oidc: true,
requestAccessToken: true,
strictDiscoveryDocumentValidation: false,
showDebugInformation: true,
};
private isAuthenticated = new Subject<boolean>();
private user: User | null = null;
private oauthService: OAuthService = inject(OAuthService);
constructor() {
this.oauthService.configure(this.authConfig);
this.oauthService.events.subscribe((event) => {
if (event.type === 'token_received') {
this.oauthService.loadUserProfile().then((profile) => {
this.fromUserProfile(profile).subscribe((user) => {
this.user = user;
});
});
}
});
this.oauthService.loadDiscoveryDocumentAndTryLogin().then(() => {
this.isAuthenticated.next(this.oauthService.hasValidAccessToken());
});
@ -38,15 +56,19 @@ export class AuthService {
this.isAuthenticated.next(false);
}
getAccessToken() {
return this.oauthService.getAccessToken();
}
getUserInfo() {
return this.oauthService.loadUserProfile();
return this.user;
}
isLoggedIn() {
return this.oauthService.hasValidAccessToken();
}
private fromUserProfile(profile: object) {
return this.userService.getOrCreateUser(profile);
}
getAccessToken() {
return this.oauthService.getAccessToken();
}
}

View file

@ -1,6 +1,6 @@
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { catchError, EMPTY, Observable } from 'rxjs';
import { catchError, EMPTY, Observable, of, switchMap } from 'rxjs';
import { User } from '../model/User';
@Injectable({
@ -23,4 +23,21 @@ export class UserService {
username: username,
});
}
public getOrCreateUser(profile: any): Observable<User> {
console.log(profile);
const id = profile.info.sub;
const username = profile.info.preferred_username;
return this.getUser(id).pipe(
switchMap((user) => {
if (user) {
return of(user);
} else {
return this.createUser(id, username);
}
}),
catchError(() => EMPTY)
);
}
}