As described in here by Govind Samrow, using ajax setup to send every request with header to laravel by including:
$.ajaxSetup({
headers: { "Authorization": "myValue" }
});
However in my home page, there are anchor tag with href in order to redirect user to other page. Noticed that href is not allowed to attach header to top of that. The alternative way might be using XHR or fetch method.
I tried the XHR ajax but the purpose of XHR ajax mainly to stay on the same page instead of redirect to other page.
How can I attach header(with token value) to every href or buttons request to laravel and get verify by middleware? I struggling with the header attach part.
My middleware:
public function handle($request, Closure $next, $guard = null) {
try {
$token = $request->header('Authorization');
if ($token) {
return $next($request);
} else {
$response = array('success' => false, 'data' => null, 'detail' => array('message' => Messages::MSG_ERR_INVALID_TOKEN, 'error' => Messages::MSG_ERR_INVALID_TOKEN));
return response()->json($response);
}
} catch (Exception $ex) {
$response = array('success' => false, 'data' => null, 'detail' => array('message' => Messages::MSG_ERROR_500, 'error' => array($ex)));
return response()->json($response);
}
}