diff --git a/src/app/app.component.ts b/src/app/app.component.ts index c87102d..f7fadcf 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -87,7 +87,7 @@ export class AppComponent implements OnInit, OnDestroy { if (!isPlatformBrowser(this.platformId)) return; - this.auth.update(); + this.auth.update(localStorage.getItem('jwt')); this.subscriptions.push(this.events.listen('login', async () => await this.router.navigate(['tts-login']))); diff --git a/src/app/shared/services/api/api-authentication.service.ts b/src/app/shared/services/api/api-authentication.service.ts index 823f699..3843564 100644 --- a/src/app/shared/services/api/api-authentication.service.ts +++ b/src/app/shared/services/api/api-authentication.service.ts @@ -1,5 +1,5 @@ import { HttpClient } from '@angular/common/http'; -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import EventService from '../EventService'; import { environment } from '../../../../environments/environment'; @@ -7,16 +7,17 @@ import { environment } from '../../../../environments/environment'; providedIn: 'root' }) export class ApiAuthenticationService { + private readonly http = inject(HttpClient); + private readonly events = inject(EventService); + private authenticated: boolean; private user: any; private lastCheck: Date; - constructor(private http: HttpClient, private events: EventService) { + constructor() { this.authenticated = false; this.user = null; this.lastCheck = new Date(); - - this.events.listen('impersonation', _ => this.update()); } isAuthenticated() { @@ -44,8 +45,7 @@ export class ApiAuthenticationService { this.updateAuthenticated(false, null); } - update() { - const jwt = localStorage.getItem('jwt'); + update(jwt: string | null) { if (!jwt) { this.updateAuthenticated(false, null); return; @@ -72,7 +72,6 @@ export class ApiAuthenticationService { if (authenticated) { this.events.emit('login', null); } else { - localStorage.removeItem('jwt'); this.events.emit('logoff', null); } } diff --git a/src/app/twitch-auth-callback/twitch-auth-callback.component.ts b/src/app/twitch-auth-callback/twitch-auth-callback.component.ts index 0bc3af0..a7cb408 100644 --- a/src/app/twitch-auth-callback/twitch-auth-callback.component.ts +++ b/src/app/twitch-auth-callback/twitch-auth-callback.component.ts @@ -52,9 +52,7 @@ export class TwitchAuthCallbackComponent implements OnInit, OnDestroy { this.http.post(environment.API_HOST + '/auth/twitch/callback', { code, scope, state }) .subscribe({ next: async (response: any) => { - console.log('twitch api callback response:', response); - localStorage.setItem('jwt', response.token); - this.auth.update(); + this.auth.update(response.token); }, error: async () => await this.router.navigate(['login'], { queryParams: {