import { Injectable , Injector } from '@angular/core' ; import { joinUrl } from './utils' ; import { tap } from 'rxjs/operators' ; import { Oauth1Service } from './oauth1.service' ; import { Oauth2Service } from './oauth2.service' ; import { Observable } from 'rxjs' ; import { PopupService } from './popup.service' ; import { ConfigService } from './config.service' ; import { SharedService } from './shared.service' ; import { HttpClient } from '@angular/common/http' ; import { IOauthService } from './oauth-service' ; @ Injectable ( ) export class OauthService { readonly depProviders = [ { provide : HttpClient , useValue : this . http } , { provide : PopupService , useValue : this . popup } , { provide : ConfigService , useValue : this . config } ] ; readonly deps = [ HttpClient , PopupService , ConfigService ] ; constructor ( private http , private shared , private config , private popup ) { } public authenticate < T extends object | string > ( name , userData ? ) { const provider = this . config . options . providers [ name ] . oauthType === '1.0' ? Injector . create ( [ ... this . depProviders , { provide : Oauth1Service , deps : this . deps } ] ) . get ( Oauth1Service ) : Injector . create ( [ ... this . depProviders , { provide : Oauth2Service , deps : this . deps } ] ) . get ( Oauth2Service ) ; return provider . open < T > ( this . config . options . providers [ name ] , userData || { } ) . pipe ( tap ( response => { if ( this . config . options . providers [ name ] . url ) { this . shared . setToken ( response ) ; } } ) ) ; } public unlink < T > ( provider , url = joinUrl ( this . config . options . baseUrl , this . config . options . unlinkUrl ) , method = 'POST' ) { return this . http . request < T > ( method , url , { body : { provider } } ) ; } }