-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);
}
}
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.
– Joan Marcos