如何在laravel 5.2中将到期日期条件添加到登录功能

莎莉

在laravel 5.2中,我想添加条件,以便仅其到期日期大于今天的登录日期的用户。

protected function getCredentials(Request $request)
{

return ['email' => $request->{$this->loginUsername()}, 'password' => $request->password];

}

该代码不接受添加:'expires'=> gte(Carbon :: now())

任何帮助表示赞赏

乔纳森

我认为这是不可能的,即使在Laravel 5.5中也是如此。以看看retrieveByCredentials在方法Illuminate\Auth\EloquentUserProvider被用来从该数据库的用户,你可以看到,查询通过简单的键/值组合的where方法的$query对象,这等同于where key = value从5.5开始:

public function retrieveByCredentials(array $credentials)
{
    if (empty($credentials) ||
       (count($credentials) === 1 &&
        array_key_exists('password', $credentials))) {
        return;
    }

    // First we will add each credential element to the query as a where clause.
    // Then we can execute the query and, if we found a user, return it in a
    // Eloquent User "model" that will be utilized by the Guard instances.
    $query = $this->createModel()->newQuery();

    foreach ($credentials as $key => $value) {
        if (! Str::contains($key, 'password')) {
            $query->where($key, $value);
        }
    }

    return $query->first();
}

为了达到你所追求的,我会建议做此检查后,该用户已登录,您的控制器,例如:

// Imagine this is the controller method where you're dealing with user logins
public function login(array $credentials)
{
    if (! auth()->attempt($credentials)) {
        // Handle what happens if the users credentials are incorrect.
    }

    $user = auth()->user();

    if (Carbon::now()->gte($user->expires)) {
        // User's account has expired, lets log them out.
        auth()->logout();

        // Return a redirect with a message or something...
    }

    // Handle a successful login.
}

我不确定auth()5.2中是否提供帮助程序,但是您应该可以使用AuthFacade来执行相同的操作,例如Auth::attempt(...)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章