Search code examples
phplaravelauthenticationlaravel-middleware

Using laravel 11 cookie to manually authenticate a request


im trying to authenticate a request sent from the front end from another domain i tried to set the 'Cookie' header with the cookie value but it was blocked by the browser ,so i did the following

headers: {
                'Authorization': 'Bearer ' + sessionStorage.getItem('Cookie'),
            }

my plan now is to create a middleware and im familiar with the method

$request->bearerToken() 

but im not sure how to use the cookie i got to authenticate the request, im using laravel 11


Solution

  • since i already had the cookie, i modified the client code like this

    $.ajax({
                type: 'POST',
                url: _apiUrl,
                data: JSON.stringify(new _Model(packageName)),
                success: _receive,
                error: function(){setTimeout(function(){_send(packageName);}, 1000);},
                contentType: 'application/json',
                xhrFields: {
                    withCredentials: true
                }
            });
    

    the withCredentials flag the set the cookie header on the request allowing for laravel auth mechanism to validate this request