import { Injectable, Inject } from '@angular/core'; import { BaseService } from '@common/base/base.service'; import { BehaviorSubject, Observable, interval } from 'rxjs'; import { ConfigurationService } from '@app/core/services/configuration/configuration.service'; import { isNullOrUndefined } from 'util'; import { takeUntil } from 'rxjs/operators'; @Injectable() export class Oauth2DialogService extends BaseService{ private code: BehaviorSubject = new BehaviorSubject(undefined); constructor(private configurationService: ConfigurationService) { super(); } public registerCode(code: string) { this.code.next(code); } public login(url: string): Observable { const windows = window.open(this.configurationService.app + 'oauth2?url=' + encodeURIComponent(url) ,'', 'height=500px,width=500px'); const sub = interval(300).pipe(takeUntil(this._destroyed)).subscribe(() => { if (windows.closed) { const oauthCode = localStorage.getItem('oauthCode'); localStorage.removeItem('oauthCode'); this.code.next(oauthCode); sub.unsubscribe(); } }); return this.code.asObservable(); } }