【问题标题】:Laravel 5.2 Auth not sending cookieLaravel 5.2 Auth不发送cookie
【发布时间】:2016-12-25 07:57:43
【问题描述】:

td;dr:我正在向 auth-request 发送 JSON 响应,但缺少 session-cookie。

我正在使用类似于Illuminate\Foundation\Auth\AuthenticatesUsers 的登录方法。在我的情况下,它以 JSON 格式发送的响应并且没有呈现视图。这是控制器代码:

class DashboardController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $guard = 'web';

    public function login(LoginRequest $request)
    {
        $throttles = $this->isUsingThrottlesLoginsTrait();

        if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        $credentials = $this->getCredentials($request);
        if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
            Auth::user()->incrementLoginCounter();
            return $this->handleUserWasAuthenticated($request, $throttles);
        }

        if ($throttles && ! $lockedOut) {
            $this->incrementLoginAttempts($request);
        }
        return $this->sendFailedLoginResponse($request);
    }
}

它与 Laravel 的 Trait 基本相同。

问题:响应中没有发送 cookie。

这是我所知道的:

  • 数据库中用户表的 remember_token 列设置正确
  • 我可以用response()-> ... ->cookie('name', 'value')手动设置cookies
  • 手动设置的 cookie 在响应中正确发送
  • 保护驱动程序设置为session,提供程序设置为App\User类
  • 会话驱动是file,config是默认的laravel配置
  • Web 中间件与默认相同,Csrf-Middleware 在该端点上已关闭。

我的期望:最好的案例 - 解决问题的方法,或者任何关于我可以测试以找出它为什么不起作用的提示和想法。

【问题讨论】:

    标签: php laravel cookies laravel-5.2


    【解决方案1】:

    我自己找到了解决问题的方法,这是一件相当奇怪的事情。我所做的是打开 github 上的 laravel 存储库,并检查将在请求中加载的所有应用程序文件,并将它们与我的本地版本进行比较。这意味着路由器、控制器、中间件、提供程序等。

    RouteServiceProvider 缺少一个完整的方法。这导致应用程序告诉我 Web 中间件已应用,但实际上并未应用。

    这可能是因为之前的开发人员将 Laravel 更新到了最新版本,并且没有做好这项工作。也许是一些 Git 合并搞砸了。

    重点是 - 有时只需逐行浏览所有文件并将其与工作版本进行比较就可以了 :)

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2016-05-07
      • 2016-04-01
      • 2016-08-11
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 2015-04-18
      • 2018-01-07
      相关资源
      最近更新 更多