From 4c301bb8e1a78e90c433b4af0bc9585557d4c714 Mon Sep 17 00:00:00 2001 From: Hoang Anh Date: Tue, 27 Apr 2021 12:03:41 +0700 Subject: [PATCH] fix callback page navigate to home page --- client/src/app/app.routing.ts | 6 ++- .../modules/callback/callback.component.ts | 11 +++-- .../app/modules/callback/callback.module.ts | 2 +- .../authentication-flow.component.ts | 48 +++++++++++-------- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/client/src/app/app.routing.ts b/client/src/app/app.routing.ts index fcb3f72..c665fff 100644 --- a/client/src/app/app.routing.ts +++ b/client/src/app/app.routing.ts @@ -1,5 +1,5 @@ import {NgModule} from '@angular/core'; -import {RouterModule, Routes} from '@angular/router'; +import {PreloadAllModules, RouterModule, Routes} from '@angular/router'; import {AppComponent} from './app.component'; import {ScopeResolver} from './scope.resolver'; @@ -32,7 +32,9 @@ const routes: Routes = [ ]; @NgModule({ - imports: [RouterModule.forRoot(routes)], + imports: [RouterModule.forRoot(routes, { + preloadingStrategy: PreloadAllModules + })], exports: [RouterModule] }) export class AppRouting { diff --git a/client/src/app/modules/callback/callback.component.ts b/client/src/app/modules/callback/callback.component.ts index 6231288..bf26951 100644 --- a/client/src/app/modules/callback/callback.component.ts +++ b/client/src/app/modules/callback/callback.component.ts @@ -1,15 +1,12 @@ import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core'; import {OAuthService} from 'angular-oauth2-oidc'; -import {ActivatedRoute} from '@angular/router'; +import {ActivatedRoute, Router} from '@angular/router'; import {CookieService} from 'ngx-cookie-service'; @Component({ selector: 'app-callback', template: ` -

- callback works! -

- +

Loading...

home `, styleUrls: ['./callback.component.scss'], @@ -21,6 +18,7 @@ export class CallbackComponent implements OnInit { constructor( private oauthService: OAuthService, private route: ActivatedRoute, + private router: Router, private cookieService: CookieService, ) { } @@ -31,6 +29,9 @@ export class CallbackComponent implements OnInit { this.cookieService.set('auth_code', code); this.cookieService.set('auth_state', state); window.close(); + setTimeout(() => { + this.router.navigate(['/']); + }, 1000); } diff --git a/client/src/app/modules/callback/callback.module.ts b/client/src/app/modules/callback/callback.module.ts index 7c78e46..b4abfbc 100644 --- a/client/src/app/modules/callback/callback.module.ts +++ b/client/src/app/modules/callback/callback.module.ts @@ -9,7 +9,7 @@ const routes: Routes = [ ]; @NgModule({ - declarations: [], + declarations: [CallbackComponent], imports: [ CommonModule, RouterModule.forChild(routes) diff --git a/client/src/app/modules/home/components/authentication-flow/authentication-flow.component.ts b/client/src/app/modules/home/components/authentication-flow/authentication-flow.component.ts index 4bd4ced..2384548 100644 --- a/client/src/app/modules/home/components/authentication-flow/authentication-flow.component.ts +++ b/client/src/app/modules/home/components/authentication-flow/authentication-flow.component.ts @@ -109,6 +109,10 @@ export class AuthenticationFlowComponent implements OnInit { } ngOnInit(): void { + const code = this.cookieService.get('auth_code'); + if (code?.length) { + this.getProperties(code); + } } login(): void { @@ -119,28 +123,34 @@ export class AuthenticationFlowComponent implements OnInit { .catch() .finally(() => { const code = this.cookieService.get('auth_code'); - this.authService.exchangeToken({input: {code, redirectUrl: environment.authSetting.redirectUrl}}) - .pipe( - tap(({accessToken, expiresIn, idToken, refreshToken, tokenType}: TokenResponse) => { - this.tokenService.setToken({ - access_token: accessToken, - refresh_token: refreshToken, - scope: this.scope, - token_type: tokenType, - expires_in: expiresIn, - id_token: idToken - }); - this.cookieService.delete('auth_code'); - }), - switchMap(() => this.inventoryService.inventories()) - ).subscribe(res => { - this.properties = res; - this.hotelId.setValue(res.map(x => x.id)); - this.loadingService.setLoading(false); - }); + this.getProperties(code); }); } + private getProperties(code): void { + this.loadingService.setLoading(true); + + this.authService.exchangeToken({input: {code, redirectUrl: environment.authSetting.redirectUrl}}) + .pipe( + tap(({accessToken, expiresIn, idToken, refreshToken, tokenType}: TokenResponse) => { + this.tokenService.setToken({ + access_token: accessToken, + refresh_token: refreshToken, + scope: this.scope, + token_type: tokenType, + expires_in: expiresIn, + id_token: idToken + }); + this.cookieService.delete('auth_code'); + }), + switchMap(() => this.inventoryService.inventories()) + ).subscribe(res => { + this.properties = res; + this.hotelId.setValue(res.map(x => x.id)); + this.loadingService.setLoading(false); + }); + } + startProcess(): void { this.isProcess = true; this.processApisService.startProcess({