Reputation: 1433
I am creating advance login system in angular2. Now i am facing blocker issue. I have created gateway for api communication in angular2. This is my code
gateWay(Method, Url, data) {
console.log("gateWay "+Method)
this.Method = Method
this.Url = Url
this.data = data
if(Method == "get"){
return this.http.get('http://localhost:3000/' + Url, { headers: this.insertAuthToken }).map((res) => res.json())
.map((res) => res.json())
}else if(Method == "post"){
return this.http.post('http://localhost:3000/' + Url, JSON.stringify(data), { headers: this.insertAuthToken })
.map((res) => res.json());
}else if(Method == "put"){
return this.http.put('http://localhost:3000/' + Url, JSON.stringify(data), { headers: this.insertAuthToken })
.map((res) => res.json());
}else if(Method == "delete"){
return this.http.delete('http://localhost:3000/' + Url, { headers: this.insertAuthToken })
.map((res) => res.json());
}
} If status 200 mean i am able to see status code of response message in console.log. but if i get response as 403 mean i need to process some function in this case i am facing issue i am unable to process those function because i did subscribe error in my component
this.httpService.gateWay('get', 'v1/users/index', undefined)
.subscribe(
data => console.log(data),
error => console.log(error),
() => console.log("finished")
)
So please suggest me some idea to trigger function if i get 403 otherwise such as 400 mean need to show message in alert. This is my token setup, here i am resetting token using set Interval
SetTokenDynamically(time) {
console.log("time " + time)
clearInterval(this.timer)
this.timer = setInterval(() => {
// this.http.get('https://jsonblob.com/api/jsonBlob/56d80451e4b01190df528171')
this.http.get('http://localhost:3000/v1/users/tokenUpdate', { headers: this.headerRefreshToken })
.map((res) => res.json())
.subscribe(
data => {
Cookie.setCookie('Token2', data.token + this.a)
console.log("Call " + this.a)
this.response = data;
},
error => console.log(error),
() => {
console.log(this.response)
this.insertAuthToken = new Headers({
'AuthToken': this.response.token || ""
})
Cookie.setCookie('Authorization', this.response.token)
this.SetTokenDynamically(this.response.time_expiry_sec)
}
);
}, time);
}
Upvotes: 0
Views: 1395
Reputation: 202176
It's because in the case of a 403 status code, the map
callback won't be executed but the catch
one (and if not the error callback specified when subscribing).
this.http.get('http://localhost:3000/' + Url, {
headers: this.insertAuthToken
})
.map((res) => res.json())
.catch((res) => { // <------
if(res.status == 403){
this.SetTokenDynamically(100);
}
});
I guess that you try to dynamically add the token if you receive a 403 error and execute again the request.
Here is a sample:
@Injectable()
export class CustomHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
(...)
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
console.log('get...');
return super.get(url, options).catch(res => {
if (res.status === 403) {
// Set token in options
return super.get(url, options);
} else {
Observable.throw(res);
}
});
}
(...)
}
If you need to make a request to get the auth token you need to leverage the flatMap
operator:
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.get(url, options).catch(res => {
if (res.status === 403) {
return this.getToken().flatMap(token => {
// Set token in options
this.setToken(options);
// Execute again the request
return super.get(url, options);
}
} else {
Observable.throw(res);
}
});
Upvotes: 1