idek man
Some checks failed
CI / Get Changed Files (pull_request) Successful in 6s
CI / prettier (pull_request) Failing after 23s
CI / Checkstyle Main (pull_request) Successful in 44s
CI / test-build (pull_request) Failing after 55s
CI / eslint (pull_request) Failing after 57s

This commit is contained in:
csimonis 2025-03-13 12:28:52 +01:00 committed by Constantin Simonis
parent e848b548b5
commit 242b72ca45
No known key found for this signature in database
GPG key ID: 3878FF77C24AF4D2
7 changed files with 55 additions and 26 deletions

View file

@ -5,9 +5,27 @@ import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { routes } from './app.routes';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
import { provideOAuthClient } from 'angular-oauth2-oidc';
import { OAuthStorage, provideOAuthClient } from 'angular-oauth2-oidc';
import { httpInterceptor } from './shared/interceptor/http.interceptor';
function storageFactory() {
return new class implements OAuthStorage {
private data: { [key: string]: string } = {};
getItem(key: string): string | null {
return this.data[key];
}
removeItem(key: string): void {
delete this.data[key]
}
setItem(key: string, data: string): void {
this.data[key] = data;
}
}
}
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
@ -16,5 +34,9 @@ export const appConfig: ApplicationConfig = {
provideExperimentalZonelessChangeDetection(),
provideAnimationsAsync(),
provideOAuthClient(),
{
provide: OAuthStorage,
useFactory: () => storageFactory(),
}
],
};

View file

@ -15,6 +15,5 @@ export default class LoginSuccessComponent implements OnInit {
private router: Router = inject(Router);
private authService: AuthService = inject(AuthService);
async ngOnInit() {
console.log(this.authService.getAccessToken());
}
}

View file

@ -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']);
});
});
}

View file

@ -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);
}
}
}

View file

@ -1,8 +1,18 @@
import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { OAuthStorage } from 'angular-oauth2-oidc';
export const httpInterceptor: HttpInterceptorFn = (req, next) => {
if (localStorage.getItem('jwt')) {
return next(req.clone({ setHeaders: { 'Authorization': 'Bearer ' + localStorage.getItem('jwt') } }));
const oauthStorage = inject(OAuthStorage);
if (oauthStorage.getItem('jwt')) {
return next(req.clone({
setHeaders: {
'Authorization': 'Bearer ' + oauthStorage.getItem('jwt'),
'Access-Control-Allow-Origin': '*',
'Referrer-Policy': 'no-referrer',
}
}));
} else {
return next(req);
}