Skip to content

Commit 3f44eca

Browse files
committed
fix(sample): use hash-based routing
1 parent 8fa99ff commit 3f44eca

File tree

12 files changed

+591
-256
lines changed

12 files changed

+591
-256
lines changed

package-lock.json

+512-214
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+16-16
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
},
2121
"private": true,
2222
"dependencies": {
23-
"@angular/animations": "9.0.5",
24-
"@angular/common": "9.0.5",
25-
"@angular/compiler": "9.0.5",
26-
"@angular/core": "9.0.5",
27-
"@angular/elements": "9.0.5",
28-
"@angular/forms": "9.0.5",
29-
"@angular/platform-browser": "9.0.5",
30-
"@angular/platform-browser-dynamic": "9.0.5",
31-
"@angular/router": "9.0.5",
23+
"@angular/animations": "9.0.7",
24+
"@angular/common": "9.0.7",
25+
"@angular/compiler": "9.0.7",
26+
"@angular/core": "9.0.7",
27+
"@angular/elements": "9.0.7",
28+
"@angular/forms": "9.0.7",
29+
"@angular/platform-browser": "9.0.7",
30+
"@angular/platform-browser-dynamic": "9.0.7",
31+
"@angular/router": "9.0.7",
3232
"@webcomponents/custom-elements": "^1.2.4",
3333
"angular-oauth2-oidc": "^9.0.1",
3434
"angular-oauth2-oidc-jwks": "^9.0.0",
@@ -39,16 +39,16 @@
3939
"rxjs": "6.5.4",
4040
"rxjs-compat": "^6.5.2",
4141
"text-encoder-lite": "^1.0.1",
42-
"tsickle": "^0.35.0",
42+
"tsickle": "^0.38.1",
4343
"tslib": "^1.11.1",
4444
"zone.js": "^0.10.2"
4545
},
4646
"devDependencies": {
47-
"@angular-devkit/build-angular": "~0.900.1",
48-
"@angular-devkit/build-ng-packagr": "~0.900.1",
49-
"@angular/cli": "^9.0.1",
50-
"@angular/compiler-cli": "9.0.5",
51-
"@angular/language-service": "9.0.5",
47+
"@angular-devkit/build-angular": "~0.900.7",
48+
"@angular-devkit/build-ng-packagr": "~0.900.7",
49+
"@angular/cli": "^9.0.7",
50+
"@angular/compiler-cli": "9.0.7",
51+
"@angular/language-service": "9.0.7",
5252
"@compodoc/compodoc": "^1.1.11",
5353
"@types/jasmine": "~3.3.13",
5454
"@types/jasminewd2": "~2.0.6",
@@ -68,6 +68,6 @@
6868
"protractor": "~5.4.3",
6969
"ts-node": "~8.6.2",
7070
"tslint": "~5.18.0",
71-
"typescript": "3.7.5"
71+
"typescript": "^3.7.5"
7272
}
7373
}

projects/quickstart-demo/src/app/app.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { HttpClientModule } from '@angular/common/http';
1515
AppComponent
1616
],
1717
providers: [
18-
{ provide: OAuthStorage, useValue: localStorage }
18+
// { provide: OAuthStorage, useValue: localStorage }
1919
],
2020
bootstrap: [AppComponent]
2121
})

projects/sample/src/app/app.component.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Router } from '@angular/router';
66
import { filter} from 'rxjs/operators';
77
import { authCodeFlowConfig } from './auth-code-flow.config';
88
import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks';
9+
import { useHash } from '../flags';
910

1011
@Component({
1112
// tslint:disable-next-line:component-selector
@@ -41,14 +42,17 @@ export class AppComponent {
4142

4243
}
4344

44-
4545
private configureImplicitFlow() {
4646

4747
this.oauthService.configure(authConfig);
4848
// this.oauthService.setStorage(localStorage);
4949
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
5050

51-
this.oauthService.loadDiscoveryDocumentAndTryLogin();
51+
this.oauthService.loadDiscoveryDocumentAndTryLogin().then(_ => {
52+
if (useHash) {
53+
this.router.navigate(['/']);
54+
}
55+
});
5256

5357
// Optional
5458
this.oauthService.setupAutomaticSilentRefresh();

projects/sample/src/app/app.module.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,34 @@ import { OAuthModule, OAuthStorage } from 'angular-oauth2-oidc';
55
import { HttpClientModule } from '@angular/common/http';
66

77
import { AppComponent } from './app.component';
8-
import { AppRouterModule } from './app.routes';
8+
import { APP_ROUTES } from './app.routes';
99
import { BASE_URL } from './app.tokens';
1010
import { FlightHistoryComponent } from './flight-history/flight-history.component';
1111
import { HomeComponent } from './home/home.component';
1212
import { PasswordFlowLoginComponent } from './password-flow-login/password-flow-login.component';
1313
import { SharedModule } from './shared/shared.module';
14+
import { RouterModule, ExtraOptions } from '@angular/router';
15+
import { CustomPreloadingStrategy } from './shared/preload/custom-preloading.strategy';
16+
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
17+
import { useHash } from '../flags';
18+
19+
20+
const ROUTING_OPTIONS: ExtraOptions = {
21+
// preloadingStrategy: CustomPreloadingStrategy,
22+
useHash: useHash,
23+
initialNavigation: !useHash
24+
};
1425

1526
@NgModule({
1627
imports: [
1728
BrowserModule,
29+
RouterModule.forRoot(APP_ROUTES, ROUTING_OPTIONS),
1830
FormsModule,
1931
ReactiveFormsModule,
2032
HttpClientModule,
2133
SharedModule.forRoot(),
22-
AppRouterModule,
2334
OAuthModule.forRoot({
24-
resourceServer: {
35+
resourceServer: {
2536
allowedUrls: ['http://www.angular.at/api'],
2637
sendAccessToken: true
2738
}
@@ -34,6 +45,7 @@ import { SharedModule } from './shared/shared.module';
3445
PasswordFlowLoginComponent,
3546
],
3647
providers: [
48+
// (useHash) ? { provide: LocationStrategy, useClass: HashLocationStrategy } : [],
3749
// {provide: AuthConfig, useValue: authConfig },
3850
// { provide: OAuthStorage, useValue: localStorage },
3951
// { provide: ValidationHandler, useClass: JwksValidationHandler },

projects/sample/src/app/app.routes.ts

-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { PasswordFlowLoginComponent } from './password-flow-login/password-flow-
22
import { Routes, RouterModule } from '@angular/router';
33
import { HomeComponent } from './home/home.component';
44
import { FlightHistoryComponent } from './flight-history/flight-history.component';
5-
import { CustomPreloadingStrategy } from './shared/preload/custom-preloading.strategy';
65

76
export let APP_ROUTES: Routes = [
87
{
@@ -32,9 +31,3 @@ export let APP_ROUTES: Routes = [
3231
redirectTo: 'home'
3332
}
3433
];
35-
36-
export let AppRouterModule = RouterModule.forRoot(APP_ROUTES, {
37-
preloadingStrategy: CustomPreloadingStrategy,
38-
useHash: localStorage.getItem('useHashLocationStrategy') === 'true',
39-
// initialNavigation: false
40-
});

projects/sample/src/app/auth-code-flow.config.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { AuthConfig } from 'angular-oauth2-oidc';
2-
3-
// Set this to true, to use silent refresh; otherwise the example
4-
// uses the refresh_token via an AJAX coll to get new tokens.
5-
const useSilentRefresh = false;
2+
import { useSilentRefreshForCodeFlow } from '../flags';
63

74
export const authCodeFlowConfig: AuthConfig = {
85
issuer: 'https://idsvr4.azurewebsites.net',
@@ -29,7 +26,7 @@ export const authCodeFlowConfig: AuthConfig = {
2926
// The first four are defined by OIDC.
3027
// Important: Request offline_access to get a refresh token
3128
// The api scope is a usecase specific one
32-
scope: (useSilentRefresh) ?
29+
scope: (useSilentRefreshForCodeFlow) ?
3330
'openid profile email api' :
3431
'openid profile email offline_access api',
3532

@@ -43,7 +40,7 @@ export const authCodeFlowConfig: AuthConfig = {
4340
silentRefreshRedirectUri:
4441
`${window.location.origin}/silent-refresh.html`,
4542

46-
useSilentRefresh: useSilentRefresh,
43+
useSilentRefresh: useSilentRefreshForCodeFlow,
4744

4845
showDebugInformation: true,
4946

projects/sample/src/app/auth.config.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ export const authConfig: AuthConfig = {
77
issuer: 'https://idsvr4.azurewebsites.net',
88

99
// URL of the SPA to redirect the user to after login
10-
redirectUri: window.location.origin
11-
+ ((localStorage.getItem('useHashLocationStrategy') === 'true')
12-
? '/#/index.html'
13-
: '/index.html'),
10+
// redirectUri: window.location.origin
11+
// + ((localStorage.getItem('useHashLocationStrategy') === 'true')
12+
// ? '/#/index.html'
13+
// : '/index.html'),
14+
15+
redirectUri: window.location.origin + '/index.html',
1416

1517
// URL of the SPA to redirect the user after silent refresh
1618
silentRefreshRedirectUri: window.location.origin + '/silent-refresh.html',

projects/sample/src/app/flight-booking/flight-search/flight-search.component.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,6 @@ <h1>Flight Search!</h1>
121121
----------------------
122122
{{selectedFlight | json}}
123123
</pre>
124-
</div>
124+
</div>
125+
126+
<button class="btn btn-primary" (click)="refresh()">Refresh Token</button>

projects/sample/src/app/flight-booking/flight-search/flight-search.component.ts

+18
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,22 @@ export class FlightSearchComponent {
3434

3535
// .map(function(resp) { return resp.json() })
3636
}
37+
38+
refresh() {
39+
40+
this.oauthService.oidc = true;
41+
42+
if (!this.oauthService.useSilentRefresh && this.oauthService.responseType === 'code') {
43+
this.oauthService
44+
.refreshToken()
45+
.then(info => console.debug('refresh ok', info))
46+
.catch(err => console.error('refresh error', err));
47+
} else {
48+
this.oauthService
49+
.silentRefresh()
50+
.then(info => console.debug('silent refresh ok', info))
51+
.catch(err => console.error('silent refresh error', err));
52+
}
53+
}
54+
3755
}

projects/sample/src/app/home/home.component.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export class HomeComponent implements OnInit {
1515
}
1616

1717
ngOnInit() {
18+
// This would directly (w/o user interaction) redirect the user to the
19+
// login page if they are not already logged in.
1820
/*
1921
this.oauthService.loadDiscoveryDocumentAndTryLogin().then(_ => {
2022
if (!this.oauthService.hasValidIdToken() || !this.oauthService.hasValidAccessToken()) {
@@ -101,8 +103,8 @@ export class HomeComponent implements OnInit {
101103
} else {
102104
this.oauthService
103105
.silentRefresh()
104-
.then(info => console.debug('refresh ok', info))
105-
.catch(err => console.error('refresh error', err));
106+
.then(info => console.debug('silent refresh ok', info))
107+
.catch(err => console.error('silent refresh error', err));
106108
}
107109
}
108110

projects/sample/src/flags.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
// Use HashLocationStrategy for routing?
3+
export const useHash = false;
4+
5+
// Set this to true, to use silent refresh; otherwise the example
6+
// uses the refresh_token via an AJAX coll to get new tokens.
7+
export const useSilentRefreshForCodeFlow = false;

0 commit comments

Comments
 (0)