Skip to content

Commit

Permalink
fix: Add better error handling #18
Browse files Browse the repository at this point in the history
  • Loading branch information
jabali2004 committed Jul 18, 2021
1 parent a656f22 commit d526ff6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions projects/strapi-auth/src/lib/interceptors/auth.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
HttpRequest,
HttpErrorResponse
} from '@angular/common/http';
import { throwError, Observable, BehaviorSubject, of } from 'rxjs';
import { catchError, filter, take, switchMap } from 'rxjs/operators';
import { throwError, Observable} from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AuthService } from '../services/auth.service';
import { StrapiAuthConfig } from '../types/StrapiAuthConfig';
import { ConfigService } from '../services/config.service';
Expand All @@ -22,6 +22,7 @@ export class AuthInterceptor implements HttpInterceptor {
) {}

private AUTH_HEADER = 'Authorization';

private token;
private authService: AuthService;

Expand All @@ -45,14 +46,21 @@ export class AuthInterceptor implements HttpInterceptor {
switch (error.status) {
// Intercept unauthorized request
case 401:
if (error.error.message === 'Invalid token.') {
// Check if error response is caused by invalid token
if (error.error.message === 'Invalid token.' && error.error.error === 'Unauthorized') {
return this.authService.logout().then(() => {
this.router.navigateByUrl('/auth/login');
});
} else {
return throwError(error);
}

case 403:
return throwError(error);

case 404:
return throwError(error);

default:
return throwError(error);
}
Expand Down

0 comments on commit d526ff6

Please sign in to comment.