0
votes

I need to refresh my token, but HttpErrorResponse does not return the result of the request.

get http://127.0.0.1:8000/api/pdv return response : {"token_error":"token_expired"}

but, when my token is expired I receive (Cross-Origin Request Blocked). just this case.

here is my config
config/cors I'm used barryvdh/laravel-cors

'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedOriginsPatterns' => [],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['*'],
    'exposedHeaders' => [],
    'maxAge' => 0,

middlewareGroups

'api' => [
    \Barryvdh\Cors\HandleCors::class,
    'throttle:60,1',
    'bindings',
],

I try get response with (obs: console.log(error.error) is undefined)

const error = (typeof errorResponse.error !== 'object') ? JSON.parse(errorResponse.error) : errorResponse.error;
                    console.log(error.error)

        return next.handle(request)
            .pipe(
                catchError((errorResponse: HttpErrorResponse) => {
                    // return next.handle(request)
                    const error = (typeof errorResponse.error !== 'object') ? JSON.parse(errorResponse.error) : errorResponse.error;
                    console.log(error.error)
                    // return ;
                    // || (errorResponse.status === 0)
                    if (errorResponse.status === 401 && error.error === 'token_expired') {
                        const http = this.injector.get(HttpClient);
                        return http.post<any>(`${API}/auth/refresh`, {})
                            .pipe(
                                flatMap(data => {
                                    localStorage.setItem("token", data.token)
                                    const authRequest = request.clone({ setHeaders: { 'Authorization': `Bearer ${data.token}` } })
                                    return next.handle(authRequest)
                                })
                            )
                    }

                    return throwError(errorResponse);
                })
            )

    }

I need to get this return "token_expired" then refresh token.

console.log(error.error)

enter image description here

console.log(errorResponse)

enter image description here

1
Do you mean console.log(error.error) prints nothing? - Sumit Parakh
Do you get any response at all? Check the browsers developer console and network tab. This might be a CORS issue. About the same origin policy. - frido
@SumitParakh yes, print undefined. - Herick
So the error code part is hit though? Can you post the error exactly how you receive it? - AJT82
@AJT_82 just error.error is undefined. but api response is {"token_error":"token_expired"} i need get this response. - Herick

1 Answers

0
votes

It seems you are already parsing the error object and then saving the parsed result to error variable. So in that case, why do you need to use error.error ? Can't you just use error variable like this?:--

Replace

console.log(error.error);

By

console.log(error);