0

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);
}
}
JohnnyCc
  • 525
  • 1
  • 8
  • 23
  • Are the routes that serve your pages in the `web` middleware group or the `api` middleware group? The routes that serve pages (web) shouldn't require an Authorization header because they authenticate with cookies. – Travis Britz Feb 13 '19 at 04:21
  • hi @TravisBritz, the middleware is custom created and used for web route. I didn't register it under web or api group, just under routemiddleware. – JohnnyCc Feb 13 '19 at 04:56

0 Answers0