idek man
Some checks failed
Some checks failed
This commit is contained in:
parent
e848b548b5
commit
242b72ca45
7 changed files with 55 additions and 26 deletions
|
@ -1,16 +1,15 @@
|
|||
import { inject, Injectable } from '@angular/core';
|
||||
import { Subject } from 'rxjs';
|
||||
import { AuthConfig, OAuthService } from 'angular-oauth2-oidc';
|
||||
import { AuthConfig, OAuthService, OAuthStorage } from 'angular-oauth2-oidc';
|
||||
import { UserService } from './user.service';
|
||||
import { User } from '../model/User';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthService {
|
||||
private userService: UserService = inject(UserService);
|
||||
|
||||
private readonly authConfig: AuthConfig = {
|
||||
issuer: 'https://oauth.simonis.lol/application/o/casino-dev/',
|
||||
clientId: 'MDqjm1kcWKuZfqHJXjxwAV20i44aT7m4VhhTL3Nm',
|
||||
|
@ -21,24 +20,34 @@ export class AuthService {
|
|||
oidc: true,
|
||||
requestAccessToken: true,
|
||||
strictDiscoveryDocumentValidation: false,
|
||||
showDebugInformation: true,
|
||||
skipIssuerCheck: true,
|
||||
disableAtHashCheck: true,
|
||||
};
|
||||
|
||||
private userService: UserService = inject(UserService);
|
||||
private oauthService: OAuthService = inject(OAuthService);
|
||||
private oauthStorage: OAuthStorage = inject(OAuthStorage);
|
||||
private router: Router = inject(Router);
|
||||
|
||||
private isAuthenticated = new Subject<boolean>();
|
||||
private user: User | null = null;
|
||||
private oauthService: OAuthService = inject(OAuthService);
|
||||
|
||||
constructor() {
|
||||
console.log(1);
|
||||
this.oauthService.setStorage(localStorage);
|
||||
this.oauthService.configure(this.authConfig);
|
||||
this.oauthService.events.subscribe((event) => {
|
||||
console.log(2, event.type);
|
||||
if (event.type === 'token_received') {
|
||||
localStorage.setItem('jwt', this.getAccessToken());
|
||||
console.log(3);
|
||||
this.oauthStorage.setItem('jwt', this.getAccessToken());
|
||||
this.oauthService.loadUserProfile().then((profile) => {
|
||||
console.log(4);
|
||||
this.fromUserProfile(profile).subscribe((user) => {
|
||||
console.log(5);
|
||||
this.user = user;
|
||||
console.log(user);
|
||||
this.router.navigate(['home']);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -25,19 +25,12 @@ export class UserService {
|
|||
}
|
||||
|
||||
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)
|
||||
);
|
||||
try {
|
||||
return this.getUser(id) as Observable<User>;
|
||||
} catch (error) {
|
||||
return this.createUser(id, username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue