Skip to content

Commit 54932f0

Browse files
committed
chore: handle unauthorized
1 parent 6f53701 commit 54932f0

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Solution 6 - OIDC and Angular client/ClientApp/ClientApp/src/app/app.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { CoreModule } from './core/core.module';
3030
{ path: 'counter', component: CounterComponent },
3131
{ path: 'fetch-data', component: FetchDataComponent },
3232
{ path: 'unauthorized', component: UnauthorizedComponent },
33+
{ path: 'forbidden', component: UnauthorizedComponent },
3334
{ path: '**', redirectTo: '' }
3435
]),
3536
CoreModule

Solution 6 - OIDC and Angular client/ClientApp/ClientApp/src/app/core/auth/auth.service.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Injectable, OnInit, OnDestroy, Inject } from '@angular/core';
1+
import { Injectable, OnDestroy, Inject } from '@angular/core';
22
import { OidcSecurityService, OpenIdConfiguration, AuthWellKnownEndpoints, AuthorizationResult, AuthorizationState } from 'angular-auth-oidc-client';
3-
import { Observable , Subscription } from 'rxjs';
3+
import { Observable , Subscription, throwError } from 'rxjs';
4+
import { catchError } from 'rxjs/operators';
45
import { HttpHeaders, HttpClient } from '@angular/common/http';
56
import { Router } from '@angular/router';
67

@@ -113,21 +114,37 @@ export class AuthService implements OnDestroy {
113114
}
114115

115116
get(url: string): Observable<any> {
116-
return this.http.get(url, { headers: this.getHeaders() });
117+
return this.http.get(url, { headers: this.getHeaders() })
118+
.pipe(catchError((error) => {
119+
this.oidcSecurityService.handleError(error);
120+
return throwError(error);
121+
}));
117122
}
118123

119124
put(url: string, data: any): Observable<any> {
120125
const body = JSON.stringify(data);
121-
return this.http.put(url, body, { headers: this.getHeaders() });
126+
return this.http.put(url, body, { headers: this.getHeaders() })
127+
.pipe(catchError((error) => {
128+
this.oidcSecurityService.handleError(error);
129+
return throwError(error);
130+
}));
122131
}
123132

124133
delete(url: string): Observable<any> {
125-
return this.http.delete(url, { headers: this.getHeaders() });
134+
return this.http.delete(url, { headers: this.getHeaders() })
135+
.pipe(catchError((error) => {
136+
this.oidcSecurityService.handleError(error);
137+
return throwError(error);
138+
}));
126139
}
127140

128141
post(url: string, data: any): Observable<any> {
129142
const body = JSON.stringify(data);
130-
return this.http.post(url, body, { headers: this.getHeaders() });
143+
return this.http.post(url, body, { headers: this.getHeaders() })
144+
.pipe(catchError((error) => {
145+
this.oidcSecurityService.handleError(error);
146+
return throwError(error);
147+
}));
131148
}
132149

133150
private getHeaders() {

0 commit comments

Comments
 (0)