@if (showImpersonation) {
diff --git a/src/app/navigation/topbar/topbar.component.ts b/src/app/navigation/topbar/topbar.component.ts
index 816bc12..b67eb34 100644
--- a/src/app/navigation/topbar/topbar.component.ts
+++ b/src/app/navigation/topbar/topbar.component.ts
@@ -51,7 +51,7 @@ export class Topbar implements OnDestroy {
}
get isAdminLoggedIn() {
- return this.auth.isAuthenticated() && this.auth.isAdmin();
+ return this.auth.isAdmin();
}
get username() {
diff --git a/src/app/shared/services/api/api-authentication.service.ts b/src/app/shared/services/api/api-authentication.service.ts
index 28dc3bb..823f699 100644
--- a/src/app/shared/services/api/api-authentication.service.ts
+++ b/src/app/shared/services/api/api-authentication.service.ts
@@ -1,6 +1,7 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import EventService from '../EventService';
+import { environment } from '../../../../environments/environment';
@Injectable({
providedIn: 'root'
@@ -50,26 +51,28 @@ export class ApiAuthenticationService {
return;
}
- // /api/auth/validate
- this.http.get('/api/auth/validate', {
+ this.http.get(environment.API_HOST + '/auth/validate', {
headers: {
'Authorization': 'Bearer ' + jwt
- }
- }).subscribe((data: any) => {
- this.updateAuthenticated(data?.authenticated, data?.user);
+ },
+ withCredentials: true
+ }).subscribe({
+ next: (data: any) => this.updateAuthenticated(data?.authenticated, data?.user),
+ error: () => this.updateAuthenticated(false, null)
});
}
private updateAuthenticated(authenticated: boolean, user: any) {
const previous = this.authenticated;
- this.authenticated = authenticated;
this.user = user;
+ this.authenticated = authenticated;
this.lastCheck = new Date();
if (previous != authenticated) {
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 5fb0ddc..0bc3af0 100644
--- a/src/app/twitch-auth-callback/twitch-auth-callback.component.ts
+++ b/src/app/twitch-auth-callback/twitch-auth-callback.component.ts
@@ -1,9 +1,10 @@
import { isPlatformBrowser } from '@angular/common';
import { HttpClient } from '@angular/common/http';
-import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core';
+import { Component, inject, Inject, OnDestroy, OnInit, PLATFORM_ID } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ApiAuthenticationService } from '../shared/services/api/api-authentication.service';
import { environment } from '../../environments/environment';
+import { Subscription } from 'rxjs';
@Component({
selector: 'app-twitch-auth-callback',
@@ -12,20 +13,26 @@ import { environment } from '../../environments/environment';
templateUrl: './twitch-auth-callback.component.html',
styleUrl: './twitch-auth-callback.component.scss'
})
-export class TwitchAuthCallbackComponent implements OnInit {
- private isBrowser: boolean;
+export class TwitchAuthCallbackComponent implements OnInit, OnDestroy {
+ private readonly auth = inject(ApiAuthenticationService);
+ private readonly http = inject(HttpClient);
+ private readonly route = inject(ActivatedRoute);
+ private readonly router = inject(Router);
+ private readonly subscriptions: (Subscription | null)[] = [];
- constructor(private http: HttpClient, private auth: ApiAuthenticationService, private route: ActivatedRoute, private router: Router, @Inject(PLATFORM_ID) private platformId: Object) {
- this.isBrowser = isPlatformBrowser(this.platformId)
- }
+ constructor(@Inject(PLATFORM_ID) private platformId: Object) { }
async ngOnInit(): Promise
{
- if (!this.isBrowser) {
+ if (!isPlatformBrowser(this.platformId)) {
return;
}
- if (this.auth.isAuthenticated()) {
- await this.router.navigate(['tts-login']);
+ if (!this.auth.isAuthenticated() && localStorage.getItem('jwt')) {
+ localStorage.removeItem('jwt');
+ }
+
+ if (this.auth.isAuthenticated() || localStorage.getItem('jwt')) {
+ await this.router.navigate(['policies']);
return;
}
@@ -43,20 +50,24 @@ export class TwitchAuthCallbackComponent implements OnInit {
}
this.http.post(environment.API_HOST + '/auth/twitch/callback', { code, scope, state })
- .subscribe(async (response: any) => {
- if (!response?.authenticated) {
- await this.router.navigate(['login'], {
- queryParams: {
- error: 'callback_issue'
- }
- });
- return;
- }
-
- localStorage.setItem('jwt', response.token);
- this.auth.update();
-
- await this.router.navigate(['tts-login']);
+ .subscribe({
+ next: async (response: any) => {
+ console.log('twitch api callback response:', response);
+ localStorage.setItem('jwt', response.token);
+ this.auth.update();
+ },
+ error: async () => await this.router.navigate(['login'], {
+ queryParams: {
+ error: 'callback_issue_twitch'
+ }
+ }),
});
}
+
+ ngOnDestroy(): void {
+ for (let subscription of this.subscriptions) {
+ if (subscription)
+ subscription.unsubscribe();
+ }
+ }
}
diff --git a/src/app/twitch-users/twitch-user-item-add/twitch-user-item-add.component.ts b/src/app/twitch-users/twitch-user-item-add/twitch-user-item-add.component.ts
index 44fe324..e5acb1c 100644
--- a/src/app/twitch-users/twitch-user-item-add/twitch-user-item-add.component.ts
+++ b/src/app/twitch-users/twitch-user-item-add/twitch-user-item-add.component.ts
@@ -10,6 +10,7 @@ import { Group } from '../../shared/models/group';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
+import { environment } from '../../../environments/environment';
@Component({
selector: 'app-twitch-user-item-add',
@@ -49,7 +50,7 @@ export class TwitchUserItemAddComponent implements OnInit {
this.responseError = undefined;
const username = this.usernameControl.value!.toLowerCase();
- this.http.get('/api/auth/twitch/users?login=' + username, {
+ this.http.get(environment.API_HOST + '/auth/twitch/users?login=' + username, {
headers: {
'x-api-key': this.client.api_key,
}