Skip to content

Commit e04326c

Browse files
committed
feat(core): add auth interceptor
pass headers to http request headers via interceptor make repeated requests on error
1 parent 2a0acaf commit e04326c

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

.ToDo

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ BrowserClient:
1515
@critical @today
1616
☐ perf: ngFor + trackBy @created(18-06-13 21:57:46)
1717

18-
@critical @today
19-
☐ feat(core): catch errors/pass headers via interceptor @created(18-06-13 21:55:41)
20-
custom errorhandler (0rl1on)
21-
https://angular.io/api/common/http/HttpInterceptor
22-
https://www.youtube.com/watch?v=z9U_xYSsVRw
23-
2418
@critical @feature
2519
☐ feat(core): notifications - display notification error, when api doesn't available @created(18-06-13 21:27:50)
2620

@@ -63,6 +57,11 @@ BrowserClient:
6357
https://offering.solutions/blog/articles/2018/02/10/separating-state-into-angular-modules-with-ngrx/
6458

6559
Archive:
60+
✔ feat(core): catch errors/pass headers via interceptor @created(18-06-13 21:55:41) @done(18-06-30 12:23:37) @project(BrowserClient.In Progress)
61+
custom errorhandler (0rl1on)
62+
https://angular.io/api/common/http/HttpInterceptor
63+
https://www.youtube.com/watch?v=z9U_xYSsVRw
64+
6665
✔ create page submodules with lazyloading for feature modules if need
6766

6867
Info:

src/app/core/core.module.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
import { HTTP_INTERCEPTORS } from '@angular/common/http';
12
import { NgModule } from '@angular/core';
23

4+
import { AuthInterceptor } from './services/auth.interceptor';
5+
6+
const authInterceptor = {
7+
multi: true,
8+
provide: HTTP_INTERCEPTORS,
9+
useClass: AuthInterceptor,
10+
};
11+
12+
const providers = [authInterceptor];
13+
314
@NgModule({
4-
exports: [],
5-
imports: [],
6-
providers: [],
15+
providers,
716
})
8-
export class CoreModule { }
17+
export class CoreModule {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
2+
import { Injectable } from '@angular/core';
3+
import { Observable } from 'rxjs';
4+
import { delay, retryWhen, scan } from 'rxjs/operators';
5+
6+
@Injectable()
7+
export class AuthInterceptor implements HttpInterceptor {
8+
intercept(request: HttpRequest<{}>, next: HttpHandler): Observable<HttpEvent<{}>> {
9+
const newRequest = request.clone({
10+
setHeaders: {
11+
Authorization: `Bearer ...xAuthToken...`,
12+
},
13+
});
14+
15+
return next.handle(newRequest).pipe(
16+
retryWhen(err =>
17+
err.pipe(
18+
scan(retryCount => {
19+
if (retryCount < 2) {
20+
return retryCount + 1;
21+
}
22+
throw new Error('Retry limit exceeded!');
23+
}, 0), // tslint:disable-line:align
24+
delay(2000),
25+
),
26+
),
27+
);
28+
}
29+
}

0 commit comments

Comments
 (0)