import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { TokenResponse } from '@core/typings/token.typing'; import { PortalDeterminationService } from '../portal-determination.service'; import { TokenAdapterService } from './token-adapter.service'; @Injectable({ providedIn: 'root' }) export class TokenRefreshResources { constructor ( private http: HttpClient, private portal: PortalDeterminationService, private adapter: TokenAdapterService ) { } protected refreshApplicantToken (refreshToken: string, userId: number, clientIdentifier: string) { return this.http.post('/api/token/Refresh/Portal', { refreshToken, userId, clientIdentifier }).toPromise(); } protected refreshManagerToken (refreshToken: string, userId: number, clientIdentifier: string) { return this.http.post('/api/token/Refresh/Manager', { refreshToken, userId, clientIdentifier }).toPromise(); } protected refreshAdminToken (refreshToken: string, userId: number, clientIdentifier: string) { return this.http.post('/api/token/Refresh/Admin', { refreshToken, userId, clientIdentifier }).toPromise(); } async refreshToken ( refreshToken: string, userId: number, clientIdentifier: string ): Promise { let response: TokenResponse; if (this.portal.isPlatform) { response = await this.refreshAdminToken(refreshToken, userId, clientIdentifier); } else if (this.portal.isApply) { response = await this.refreshApplicantToken(refreshToken, userId, clientIdentifier); } else { response = await this.refreshManagerToken(refreshToken, userId, clientIdentifier); } try { return this.adapter.handleTokenRequest(response); } catch (err) { const e = err as HttpErrorResponse; if (e.status === 400) { return null; } throw e; } } }