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

40 lines
1.4 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 '@app/utilities/enhancers/utils';
import { takeUntil } from 'rxjs/operators';
@Injectable()
export class Oauth2DialogService extends BaseService{
private code: BehaviorSubject<any> = new BehaviorSubject(undefined);
constructor(private configurationService: ConfigurationService) {
super();
}
public registerCode(code: any) {
this.code.next(code);
}
public login(url: string): Observable<any> {
const windows = window.open(this.configurationService.app + 'oauth2?url=' + encodeURIComponent(url) ,'', `height=500px,width=500px,top=${(window.screen.height / 2) - 200}px,left=${(window.screen.width / 2) - 200}px`);
const sub = interval(300).pipe(takeUntil(this._destroyed)).subscribe(() => {
if (windows.closed) {
if (localStorage.getItem('arglogRes')) {
const oauthResponse = JSON.parse(localStorage.getItem('arglogRes'));
localStorage.removeItem('arglogRes');
const oauthObject = oauthResponse.response.data;
this.code.next(oauthObject);
this.code.next(undefined);
}
sub.unsubscribe();
}
});
return this.code.asObservable();
}
}