Laravel auth CORS issue

无人久伴 提交于 2020-02-08 09:13:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!