I need to handle 401
error correctly on my Ionic 3
app. The below mentioned error comes when the app has not been used for a few days and after that user tries to access the app. This app has a backend web API(django) and it uses jwt
token to validate the user.
So can you tell me the correct workflow of handling this kind of use case on Ionic 3
app? I was unable to find out good resources to refer this on the web. If you have something to share, it would be great.
I have seen this URL where it mentioned about subscribe subjects
. But I don't know how to implement such thing with the Ionic app. Any clue, please?
authProvider.ts
import { Injectable, } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Headers, RequestOptions, BaseRequestOptions } from '@angular/http';
import { Storage } from '@ionic/storage';
createHeader(headers: Headers) {
return new Promise((resolve, reject) => {
this.storage.get('loggedInUser')
.then((token: any) => {
if (token) headers.append('Authorization', 'token ' + token.token);
resolve(headers);
}, err => {
resolve(headers);
});
});
}
get(api) {
return new Observable(observer => {
let header = new Headers();
this.createHeader(header)
.then(() => {
let options = new BaseRequestOptions();
options.withCredentials = true;
options.headers = header;
this.http.get(api, options)
.subscribe(response => {
observer.next(response);
observer.complete();
}, (e) => {
observer.error(e);
});
})
})
}
Console error message: