Skip to content

Commit

Permalink
fix callback page navigate to home page
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoang Anh committed Apr 27, 2021
1 parent 8f0b124 commit 4c301bb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
6 changes: 4 additions & 2 deletions client/src/app/app.routing.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -32,7 +32,9 @@ const routes: Routes = [
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
imports: [RouterModule.forRoot(routes, {
preloadingStrategy: PreloadAllModules
})],
exports: [RouterModule]
})
export class AppRouting {
Expand Down
11 changes: 6 additions & 5 deletions client/src/app/modules/callback/callback.component.ts
Original file line number Diff line number Diff line change
@@ -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: `
<p>
callback works!
</p>
<button mat-button (click)="getToken()">GetToken</button>
<p>Loading...</p>
<a routerLink="/">home</a>
`,
styleUrls: ['./callback.component.scss'],
Expand All @@ -21,6 +18,7 @@ export class CallbackComponent implements OnInit {
constructor(
private oauthService: OAuthService,
private route: ActivatedRoute,
private router: Router,
private cookieService: CookieService,
) {
}
Expand All @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion client/src/app/modules/callback/callback.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const routes: Routes = [
];

@NgModule({
declarations: [],
declarations: [CallbackComponent],
imports: [
CommonModule,
RouterModule.forChild(routes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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({
Expand Down

0 comments on commit 4c301bb

Please sign in to comment.