argos/dmp-frontend/src/app/ui/misc/oauth2-dialog/service/oauth2-dialog.service.ts

35 lines
1.1 KiB
TypeScript

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<string> = new BehaviorSubject(undefined);
constructor(private configurationService: ConfigurationService) {
super();
}
public registerCode(code: string) {
this.code.next(code);
}
public login(url: string): Observable<string> {
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();
}
}