how to refresh the access token using custom http in angular 2?

后端 未结 3 770
孤独总比滥情好
孤独总比滥情好 2021-02-06 18:50

I am using token based authentication in my application. My backend is developed using restful service(spring).The backend code is very well generating the required the access t

3条回答
  •  无人及你
    2021-02-06 19:53

    This is what worked for me:

     request(url: string | Request, options?: RequestOptionsArgs): Observable {
        //adding access token to each http request before calling super(..,..)
        let token = this.authenticationService.token;
        if (typeof url === 'string') {
            if (!options) {
                options = { headers: new Headers() };
            }
            options.headers.set('Authorization', `Bearer ${token}`);
        }
        else {
            url.headers.set('Authorization', `Bearer ${token}`);
        }
        return super.request(url, options)
          .catch((error) => {
                //if got authorization error - try to update access token
                if (error.status = 401) {
                    return this.authenticationService.updateToken()
                        .flatMap((result: boolean) => {
                            //if got new access token - retry request
                            if (result) {
                                return this.request(url, options);
                            }
                            //otherwise - throw error
                            else {
                                return Observable.throw(new Error('Can\'t refresh the token'));
                            }
    
                        })
                }
                else {
                    Observable.throw(error);
                }
            })
    }
    

    UPDATE: authenticationService.updateToken() implementation should depend on authorization provider/authorization mechanism you use. In my case it is OAuth Athorization Server, so implementation basically sends post request with refresh token in the body to configured token url and returns updated access and refresh tokens. tokenEndPointUrl is configured by OAuth and issues access and refresh tokens (depending on grant_type sent). Because i need to refresh token i set grant_type to refresh_token. Code looks similar to:

    updateToken(): Observable {
        let body: string = 'refresh_token=' + this.refreshToken + '&grant_type=refresh_token';
    
        return this.http.post(tokenEndPointUrl, body, this.options)
            .map((response: Response) => {
                var returnedBody: any = response.json();
                if (typeof returnedBody.access_token !== 'undefined'){
                  localStorage.setItem(this.tokenKey, returnedBody.access_token);
                  localStorage.setItem(this.refreshTokenKey, returnedBody.refresh_token);
                return true;
            }
            else {
                return false;
            }
            })
    }
    

    Hope it helps

提交回复
热议问题