Skip to content

Commit

Permalink
#66 use async in Interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhg committed Apr 13, 2019
1 parent f72f99a commit 52edd9e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ import {AuthService} from './core/auth-service';
export class AppModule {

constructor() {
localStorage.setItem('token', '11111');
// localStorage.setItem('token', '11111');
}
}
12 changes: 12 additions & 0 deletions src/app/core/auth-service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Injectable()
export class AuthService {


constructor(private http: HttpClient) {
}

getToken(): string {
return localStorage.getItem('token');
}

removeToken(): void {
localStorage.removeItem('token');
}

logic() {
return this.http.get('mock-data/user.json').map((res: any) => {
localStorage.setItem('token', res.token);
return res;
});
}
}
25 changes: 12 additions & 13 deletions src/app/core/token-interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Injectable} from '@angular/core';
import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {AuthService} from './auth-service';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/do';
Expand All @@ -10,25 +10,24 @@ export class TokenInterceptor implements HttpInterceptor {
}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.url === 'mock-data/user.json') {
return next.handle(request);
}

if (this.authService.getToken()) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.authService.getToken()}`
}
});
return next.handle(request);
}
return next.handle(request).do((event) => {
return event;
},
(err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
// this.authService.removeToken();
// location.reload();
}

return this.authService.logic().flatMap(() => {
return next.handle(request).do((event) => {
return event;
}
return Observable.throw(err);
}
);
);
});
}
}
6 changes: 3 additions & 3 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ export class HomeComponent implements OnInit {
});
this.test();
console.log(this.quickSort([3, 1, 2, 4, 19, 4]));
this.getUsers().subscribe(res => {
console.log(res);
});
// this.getUsers().subscribe(res => {
// console.log(res);
// });
}

getUsers() {
Expand Down
3 changes: 3 additions & 0 deletions src/mock-data/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"token": "xxxxxx"
}

0 comments on commit 52edd9e

Please sign in to comment.