Skip to content

Commit

Permalink
fix(sample): make sense of the guard
Browse files Browse the repository at this point in the history
  • Loading branch information
manfredsteyer committed Mar 24, 2020
1 parent 9710f60 commit 1cae011
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions projects/sample/src/app/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<h1 *ngIf="!givenName">Welcome!</h1>
<h1 *ngIf="givenName">Welcome, {{ givenName }} {{ familyName }}!</h1>

<div *ngIf="login" style="color: red">
You have to login before you can search for flights.
</div>

<div class="panel panel-default">
<div class="panel-body">
<p>Login with Authorization Server</p>
Expand Down
12 changes: 11 additions & 1 deletion projects/sample/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { authConfig } from '../auth.config';
import { Component, OnInit } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { authCodeFlowConfig } from '../auth-code-flow.config';
import { ActivatedRoute } from '@angular/router';

@Component({
templateUrl: './home.component.html'
Expand All @@ -10,10 +11,19 @@ export class HomeComponent implements OnInit {
loginFailed: boolean = false;
userProfile: object;
usePopup: boolean;
login: false;

constructor(private oauthService: OAuthService) {}
constructor(
private route: ActivatedRoute,
private oauthService: OAuthService) {
}

ngOnInit() {

this.route.params.subscribe(p => {
this.login = p['login'];
});

// This would directly (w/o user interaction) redirect the user to the
// login page if they are not already logged in.
/*
Expand Down
23 changes: 15 additions & 8 deletions projects/sample/src/app/shared/auth/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Injectable } from '@angular/core';
import {
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot
CanActivate, Router
} from '@angular/router';
import { OAuthService } from 'angular-oauth2-oidc';

@Injectable()
export class AuthGuard implements CanActivate {
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): boolean {
return true;

constructor(
private router: Router,
private oauthService: OAuthService) {
}

canActivate() {
if (this.oauthService.hasValidAccessToken() && this.oauthService.hasValidIdToken()) {
return true;
} else {
this.router.navigate(['/home', {login: true}]);
return false;
}
}
}

0 comments on commit 1cae011

Please sign in to comment.