繁体   English   中英

饼干罐 。 排队返回为空-Laravel

[英]Cookie Jar . Queued returns empty - Laravel

我实现了自定义守卫。 如果为true,则try方法将令牌排队到“罐子”中

我真的认为这是它的工作方式,因此,我希望获得一些反馈。

/**
 * Attempts to log-in user into the application
 *
 * @param $credentials
 * @param $rememberToken
 *
 * @return bool
 */

public function attempt($credentials, $rememberToken)
{
    try {
        $response = $this->client->login($credentials);

        Cookie::queue(
            $this->cookieName,
            $response->access_token,
            $response->expires_in
        );

        return true;

    } catch (ClientException $exception) {
        return false;
    }
}

这段代码可以正常工作。但是我想在我的自定义防护的身份验证方法中使用cookieJar检索排队的cookie。

/**
 * The user has been authenticated.
 *
 * @param Request $request
 * @param         $user
 */
protected function authenticated(Request $request, $user)
{
    $jar = new CookieJar();
    dd($jar->getQueuedCookies()); //returns empty []

}

原来cookiejar不是一个应该以我的方式实例化的类。 解决方案是调用静态方法Cookie::getQueuedCookies()以获取排队的cookie数组。 我会摘录

protected function authenticated(Request $request, $user)
{
    $cookies = Cookie::getQueuedCookies();
    $value   = $cookies[$this->cookieName]->getValue();

    $user = new User();

    $user->name     = "Customer";
    $user->lastname = "1";

    //TODO : $value has an access_token. Call the $this->guard()->setUser()

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM