Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #70 from johanngoltz/fix/http-error-handling
Browse files Browse the repository at this point in the history
Fix/http error handling
  • Loading branch information
schaefermichael authored Jul 9, 2018
2 parents ea7b6f6 + 1458706 commit e22e4f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/app/registration/registration.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<mat-card class="register-card" [class.busy]="loading">
<mat-progress-bar mode="indeterminate" color="accent"></mat-progress-bar>
<mat-card-title>Registrierung</mat-card-title>
<mat-divider></mat-divider>
<mat-card-content>
<form [formGroup]="registerFormGroup" id="register-form" (ngSubmit)="onSubmit()">
<p>
Expand Down
33 changes: 16 additions & 17 deletions src/app/service/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Injectable } from '@angular/core';
import { TypedAxiosInstance } from 'restyped-axios';
import { from, Observable, ReplaySubject } from 'rxjs';
import { tap } from 'rxjs/operators';
import { tap, switchMap, catchError } from 'rxjs/operators';
import { SumzAPI } from '../api/api';
import { HttpClient } from './http-client';

Expand Down Expand Up @@ -194,28 +194,27 @@ export class AuthenticationService {
return response;
},
error => {
if (!!error.response
&& error.response.status === 401
&& localStorage.getItem('currentUser')
&& !error.response.config.headers._isRetry) {
if (error.message === 'Network Error' ||
(!!error.response
&& error.response.status === 401
&& !error.response.config.headers._isRetry)
&& localStorage.getItem('currentUser')) {

// get new access_token
this.refresh()
.subscribe(
() => {
const newResponse = this.refresh()
.pipe(
switchMap(({ data }) => {
// update Authorization header in primary request
error.config.headers.Authorization = JSON.parse(localStorage.getItem('currentUser')).token_type
error.config.headers.Authorization = data.token_type
+ ' '
+ JSON.parse(localStorage.getItem('currentUser')).access_token;
+ data.access_token;
error.config.headers._isRetry = true;
// return the response with a new access_token

// return the response using a new access_token
return this._apiClient.request(error.config);
},
() => {
console.log('Refresh login error: ', error);
return Promise.reject(error);
}
);
})
).toPromise();
return newResponse;
}
return Promise.reject(error);
});
Expand Down

0 comments on commit e22e4f8

Please sign in to comment.