0

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

Herick
  • 217
  • 1
  • 3
  • 13
  • Do you mean console.log(error.error) prints nothing? – Sumit Parakh Jul 27 '19 at 18:45
  • Do you get any response at all? Check the browsers developer console and network tab. This might be a [CORS](https://developer.mozilla.org/de/docs/Web/HTTP/CORS) issue. [About the same origin policy](https://stackoverflow.com/a/35553666/9423231). – frido Jul 27 '19 at 19:59
  • @SumitParakh yes, print undefined. – Herick Jul 27 '19 at 20:14
  • So the error code part is hit though? Can you post the error exactly how you receive it? – AT82 Jul 27 '19 at 20:15
  • @AJT_82 just error.error is undefined. but api response is {"token_error":"token_expired"} i need get this response. – Herick Jul 27 '19 at 20:18
  • OK... so is it an error response or 200 response? How does the response look like, can you post an image? – AT82 Jul 27 '19 at 20:19
  • or, show what `console.log(errorResponse)` produces. – AT82 Jul 27 '19 at 20:21
  • @AJT_82 Console https://i.imgur.com/Zrduzb7.png – Herick Jul 27 '19 at 20:22
  • if `console.log(errorResponse)` produces `undefined` you are not getting anything clearly. Can't be... has to be a status or something. Does `console.log(errorResponse)` really produce that? – AT82 Jul 27 '19 at 20:23
  • @AJT_82 console.log(errorResponse) https://i.imgur.com/2tUT6gN.png – Herick Jul 27 '19 at 20:24
  • How are you adding the `HandleCors` middleware? Maybe it's in the wrong order. See this answer https://stackoverflow.com/a/50212896/9423231 – frido Jul 28 '19 at 13:05
  • What OAuth provider are you using? I think you have to add CORS support to those routes specifically. – frido Jul 29 '19 at 22:04

1 Answers1

0

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);
Sumit Parakh
  • 1,098
  • 9
  • 19