import { AppStorageService } from '../service/common/app-storage.service'; import { WindowRefService } from '../service/common/window.ref.service'; import { CommonService } from '../service/common/common.service'; import { SharedService } from '../service/common/shared.service'; import { Component, OnInit } from '@angular/core'; import { Http, Response, Headers, RequestOptions } from '@angular/http'; import { Location } from '@angular/common'; import { TranslateService, TranslateDirective } from '@ngx-translate/core'; import * as _ from 'lodash'; import { AppConstants } from '../common/AppConstants'; import { SessionStorageService } from '../service/common/session-storage.service'; import { Router, ActivatedRoute, Params } from '@angular/router'; import { AuthService } from '../service/common/auth.service'; import { UserService } from '../service/common/user.service'; import { environment } from '../environments/environment'; import { Subscription } from 'rxjs/Subscription'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/skip'; import { Observable } from 'rxjs/Observable' import { NavService, INavItem } from '../service/common/nav.service'; import { Title } from '@angular/platform-browser'; import { UserRoleService } from '../service/common/user-role.service'; import { CrowdinService } from '../service/common/crowdin.service'; import { RouteGuard } from '../service/common/routeguard.service'; @Component({ selector: 'app-oauthcallback', templateUrl: './oauthcallback.component.html', styleUrls: ['./oauthcallback.component.scss'] }) export class OauthcallbackComponent implements OnInit { private browserLanguage: string; private code: string; private state: string; private envName: string; private language: string; private subscription: Subscription; constructor( private translate: TranslateService, private user: UserService, private auth: AuthService, private location: Location, private router: Router, private http: Http, private route: ActivatedRoute, private sessionStorageService: SessionStorageService, private appStorageService: AppStorageService, private sharedService: SharedService, private commonService: CommonService, private windowRef: WindowRefService, private navService: NavService, private titleService: Title, private userRoleService: UserRoleService, private crowdinService: CrowdinService, private routeGuard: RouteGuard ) { this.appStorageService.set(AppConstants.LOCALE, windowRef.nativeWindow.navigator.language); this.language = window.navigator.language; // Two character language like 'en-US' this.envName = environment.envName; console.log('In App Component. Env Name = ', this.envName); } ngOnInit() { /** * if user is not authenticated and url does not includes oauth_callback then call the authorize URL * For development mimic oauth flow */ // this.subscription = this.sharedService.language$.skip(1) // .subscribe(item => { // if(this.crowdinService.IsUsingCrowdin()) { // this.language = 'zu'; // } else { // this.language = item; // } // this.translate.setDefaultLang(this.language); // console.log('Language = ', this.language); // }); // console.log('Language = ', this.language); // this.retrieveCode(); // /** // * If the user is not autheticated and location URL contains oauth_callback then call the token url // * using the code param in the URL // */ // if (!this.user.isAuthenticated && _.includes(location.href, AppConstants.OauthCallBack)) { // if (this.code && !_.isEmpty(this.code) && this.state && !_.isEmpty(this.state)) { // this.auth.getToken(this.code, this.state).subscribe(data => { // this.auth.login(data); // this.onPostAuthentication(data.subscription_info.UID).subscribe(result => { // this.finishAuthentication(result); // }); // }); // } else { // location.assign(this.routeGuard.getAuthURLWithoutHistory()); // } // } // /** // * If the user is authenticated then go to home page // */ // if (this.user.isAuthenticated && _.includes(location.href, AppConstants.OauthCallBack)) { // this.onPostAuthentication(this.user.authentication.userId).subscribe(result => { // this.finishAuthentication(result); // }); // } this.navigateToReturnUrl(); } private finishAuthentication(result: any) { this.navigateToReturnUrl(); }; private retrieveCode() { if (this.envName === 'test') { this.code = 'test'; this.state = 'test'; } this.route.queryParams.merge(this.route.params).subscribe((params: any) => { this.code = this.code || (!_.isEmpty(params['code']) ? params['code'] : null); this.state = this.state || (!_.isEmpty(params['state']) ? params['state'] : null); }); } private setInitiazationData(result: any) { this.sessionStorageService.remove(AppConstants.optionListStorageKey); this.sessionStorageService.set(AppConstants.optionListStorageKey, result[0]); } private getReturnUrl(): string { let returnUrl = this.appStorageService.get(AppConstants.ReturnUrl); this.appStorageService.remove(AppConstants.ReturnUrl); // if (!this.user.isEulaAccepted && this.envName !== 'test') { // returnUrl = ''; // } return returnUrl; } private navigateToReturnUrl(): void { // const returnUrl = this.getReturnUrl(); // console.log('Navigating to: ', returnUrl); // returnUrl !== null && !_.isEmpty(returnUrl) && returnUrl !== '/' && !_.includes(returnUrl, 'logout') // ? location.href = returnUrl : this.router.navigate(['/main']); this.router.navigate(['/main']); } private onPostAuthentication(userId: string): Observable { const o1 = this.commonService.getOptions(); const combine = Observable.zip(o1); return combine; } }