问题
I'm having the same issue with Laravel authentication and CORS as this thread:
Auth::user() returns null with CORS requests
Although, my files won't be hosted on the same domain as they will be used within a Cordova smartphone application. Does anyone have any ideas how to solve this, and still use Laravels standard Auth functionality?
Many thanks!
回答1:
Solved this by first adding the following headers to App::before function in routes:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Auth-Token, X-Request-With');
header('Access-Control-Allow-Credentials: true');
Then I changed the driver of sessions to cookie.
And I didn't preform Auth::guest() the first thing I did in the filter, but rather checked for the token if it was accessable in the User table, then connected the User-object to the Auth-object, if Auth::user was null.
来源:https://stackoverflow.com/questions/29648821/laravel-auth-cors-issue