hermes-web-angular/src/app/twitch-auth-callback/twitch-auth-callback.component.ts

49 lines
1.9 KiB
TypeScript
Raw Normal View History

import { isPlatformBrowser } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Component, Inject, 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';
@Component({
selector: 'app-twitch-auth-callback',
standalone: true,
imports: [],
templateUrl: './twitch-auth-callback.component.html',
styleUrl: './twitch-auth-callback.component.scss'
})
export class TwitchAuthCallbackComponent implements OnInit {
private isBrowser: boolean;
constructor(private http: HttpClient, private auth: ApiAuthenticationService, private route: ActivatedRoute, private router: Router, @Inject(PLATFORM_ID) private platformId: Object) {
this.isBrowser = isPlatformBrowser(this.platformId)
}
ngOnInit(): void {
if (!this.isBrowser) {
return;
}
const code = this.route.snapshot.queryParamMap.get('code');
const scope = this.route.snapshot.queryParamMap.get('scope');
const state = this.route.snapshot.queryParamMap.get('state');
console.log('twitch callback', code, scope, state);
if (!code || !scope || !state)
return;
this.http.post(environment.API_HOST + '/auth/twitch/callback', { code, scope, state })
.subscribe((data: any) => {
console.log('twitch callback response', code, scope, state, data);
if (!data?.authenticated) {
this.router.navigate(['/login?error=callback_error']);
return;
}
localStorage.setItem('jwt', data.token);
this.auth.update();
this.router.navigate(['/tts-login']);
});
}
}