我遇到了同样的障碍,因此查看代码可以看到viaRemember 并不是用来检查用户是否以用户可以使用的所有方式之一登录系统的功能登录。
'viaRemember' 用于检查用户是否专门通过 `viaRemember' cookie 登录到系统。
据我所知,用户的身份验证有两种方式:
-
via remember cookie。
cookie 值与用户表中的via remember 字段进行比较。
-
一个会话 cookie。
cookie 值在服务器中用于从
会话存储。在来自商店的会话对象上附加了数据。中的一个
数据项是连接到会话的用户 ID。第一次
会话已创建,系统将用户 ID 附加到数据
季节。
在Illuminate\Auth\Guard 类中:
public function user()
{
if ($this->loggedOut) return;
// If we have already retrieved the user for the current request we can just
// return it back immediately. We do not want to pull the user data every
// request into the method because that would tremendously slow an app.
if ( ! is_null($this->user))
{
return $this->user;
}
$id = $this->session->get($this->getName());
// First we will try to load the user using the identifier in the session if
// one exists. Otherwise we will check for a "remember me" cookie in this
// request, and if one exists, attempt to retrieve the user using that.
$user = null;
if ( ! is_null($id))
{
$user = $this->provider->retrieveByID($id);
}
// If the user is null, but we decrypt a "recaller" cookie we can attempt to
// pull the user data on that cookie which serves as a remember cookie on
// the application. Once we have a user we can return it to the caller.
$recaller = $this->getRecaller();
if (is_null($user) && ! is_null($recaller))
{
$user = $this->getUserByRecaller($recaller);
}
return $this->user = $user;
}
getUserByRecaller 函数仅在会话 cookie 身份验证不起作用时调用。
viaRemember 标志仅在 getUserByRecaller 函数中设置。 viaRemember 方法只是一个简单的 getter 方法。
public function viaRemember()
{
return $this->viaRemember;
}
所以最后,我们可以使用Auth::check() 进行所有检查,包括viaRemember 检查。它调用Guard类中的user()函数。
viaRemember 似乎也只是一个指标。您需要执行Auth::check() 的类型,这将启动身份验证过程,因此将调用 user() 函数。