53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
import { Component, inject, OnInit } from '@angular/core';
|
|
import { Router } from '@angular/router';
|
|
import { HermesClientService } from '../../hermes-client.service';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { environment } from '../../../environments/environment';
|
|
|
|
@Component({
|
|
selector: 'connection-callback',
|
|
imports: [],
|
|
templateUrl: './callback.component.html',
|
|
styleUrl: './callback.component.scss'
|
|
})
|
|
export class ConnectionCallbackComponent implements OnInit {
|
|
private readonly client = inject(HermesClientService);
|
|
private readonly http = inject(HttpClient);
|
|
private readonly router = inject(Router);
|
|
|
|
success: boolean = false;
|
|
failure: boolean = false;
|
|
|
|
async ngOnInit() {
|
|
const url = this.router.parseUrl(this.router.url);
|
|
if (!url.fragment) {
|
|
this.failure = true;
|
|
await this.router.navigate(['connections']);
|
|
return;
|
|
}
|
|
|
|
const paramsParts = url.fragment.split('&');
|
|
const params = Object.assign({}, ...paramsParts.map((p: string) => ({ [p.split('=')[0]]: p.split('=')[1] })));
|
|
|
|
if (!params.access_token || !params.scope || !params.state || !params.token_type) {
|
|
this.failure = true;
|
|
await this.router.navigate(['connections']);
|
|
return;
|
|
}
|
|
|
|
this.http.get(`${environment.API_HOST}/auth/connections?token=${params['access_token']}&state=${params['state']}&expires_in=${params['expires_in']}`).subscribe({
|
|
next: async (d: any) => {
|
|
const data = d.data;
|
|
this.success = true;
|
|
|
|
await setTimeout(async () => {
|
|
this.client.createConnection(data.connection.name, data.connection.type, data.connection.clientId, params['access_token'], data.connection.grantType, params['scope'], data.expires_at);
|
|
await this.router.navigate(['connections'])
|
|
}, 2000)
|
|
},
|
|
error: async () => await this.router.navigate(['connections'])
|
|
});
|
|
;
|
|
}
|
|
}
|