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
|
@ -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(),
|
||||
}
|
||||
],
|
||||
};
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue