Error of CORS Ionic 3

Asked

Viewed 120 times

-1

I’m trying to connect my application with a backend server in Laravel

I have an Interceptor that adds headers to the request:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('----------------- SESSION -----------------');
console.log(this.session.token);

this.token = this.session.token;
const headers = this.buildRequestHeaders();
const authRequest = req.clone({
  setHeaders: {
    'Access-Control-Allow-Credentials': 'false',
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Headers": "Content-Type",
    "Access-Control-Allow-Methods": "OPTIONS,POST,GET",
    'Authorization': `Bearer ${this.token}`
  }
});

return next.handle(authRequest);

}

Already on the server I have a middleware:

<?php

namespace App\Http\Middleware;

use App\Helpers\JsonResponseHelper;
use Closure;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Middleware\GetUserFromToken;

class JwtCheck extends GetUserFromToken
{
    public function handle($request, Closure $next)
    {
        header('Content-Type', 'application/json');
        header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Request-Headers', 'Origin, Authorization, Content-Type, Accept');
        header('Access-Control-Allow-Origin', '*');
    if (strpos($request->headers->get("Authorization"), "Bearer ") === false) {
        $request->headers->set("Authorization", "Bearer " . $request->headers->get("Authorization"));
    }

    if (!$token = $this->auth->setRequest($request)->getToken()) {
        $data['errors'] = trans('auth.failed');
        $message = trans('auth.failed');
        return JsonResponseHelper::dataResponse(trans('messages.error'), $data, true, 401, $message);
    }
    try {
        //$user = $this->auth->authenticate($token);
        $user = JWTAuth::parseToken()->authenticate($token);
    } catch (TokenExpiredException $e) {
        $data['errors'] = trans('messages.token-expired');
        $message = trans('messages.token-expired');
        return JsonResponseHelper::dataResponse(trans('messages.error'), $data, true, 401, $message);
    } catch (JWTException $e) {
        $data['errors'] = $e->getMessage();
        $message = trans('messages.error');
        return JsonResponseHelper::dataResponse(trans('messages.error'), $data, true, 401, $message);
    } catch (TokenInvalidException $e) {
        $data['errors'] = $e->getMessage();
        $message = trans('messages.error');
        return JsonResponseHelper::dataResponse(trans('messages.error'), $data, true, 401, $message);
    } catch (Exception $e) {
        $data['errors'] = $e->getMessage();
        $message = trans('messages.error');
        return JsonResponseHelper::dataResponse(trans('messages.error'), $data, true, 401, $message);
    }
    if (!$user) {
        $data['errors'] = trans('auth.failed');
        $message = trans('auth.failed');
        return JsonResponseHelper::dataResponse(trans('messages.error'), $data, true, 401, $message);
    }
    $request->merge(array("user" => $user));
    $request->merge(array("token" => $token));

    return $next($request);
    }
}

That is my problem: inserir a descrição da imagem aqui

This is the request sent to the server: inserir a descrição da imagem aqui

  • First, don’t post code as an image; the site, which is programming-oriented, perfectly supports the formation of source code, by @Andersoncarloswoss. Just leave the last two images.

1 answer

-1

If you have access to your application backend, configure the CORS in your application Windows, if you do not have and are performing localhost access, configure the proxy of your frontend, but when you build to put into production, then you would have to configure proxy on server, so I recommend setting up the CORS in your backend Windows

Browser other questions tagged

You are not signed in. Login or sign up in order to post.