Laravel auth CORS问题 [英] Laravel auth CORS issue

查看:432
本文介绍了Laravel auth CORS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了与Laravel身份验证和CORS相同的问题:

I'm having the same issue with Laravel authentication and CORS as this thread:

Auth :: user()通过CORS请求返回null

将托管在同一域上,因为它们将在Cordova智能手机应用程序中使用。任何人都有任何想法如何解决这个问题,并仍然使用Laravels标准的Auth功能?

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?

非常感谢!

推荐答案

首先将以下标题添加到路径中的App :: before函数,以解决此问题:

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');

然后我将会话的驱动程序更改为cookie。

Then I changed the driver of sessions to cookie.

我没有预先准备Auth :: guest()我在过滤器中做的第一件事,而是检查令牌,如果它是可访问的User表,然后连接用户对象到Auth-对象,如果Auth :: user为null。

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.

这篇关于Laravel auth CORS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆