【问题标题】:How can I Authenticate with custom condition in Laravel?如何在 Laravel 中使用自定义条件进行身份验证?
【发布时间】:2016-06-29 17:07:38
【问题描述】:

在 Laravel 文档中有简单的 Authentication with condition

Auth::attempt(array('email' => $email, 'password' => $password, 'active' => 1))

在我的应用中,用户可以拥有几种状态之一。我想针对其中之一进行身份验证:status != '99'。

有没有办法在这个请求中实现这一点,或者在触发 Auth 尝试之前需要一些额外的逻辑?

【问题讨论】:

  • 我不确定这是否可以在Auth::attempt() 中以您希望的方式使用。我处理自定义middleware,它随每个请求运行。可能会考虑这种方法,比如Auth::user()->status != 99 然后返回一个response() 如果以上为真,你想如何处理,否则返回默认请求。

标签: php laravel authentication laravel-5


【解决方案1】:

使用Auth::Attempt 方法检查!= 条件是不可能的,除非您覆盖EloquentServiceProvider::retrieveByCredentials 方法。

您可以在登录过程中检查User 状态,并仅在状态与99 不同时进行身份验证。

在您的检查登录方法中:

//check credentials 
if ( ! Auth::validate([ 'email' => $email, 'password' => $password ] ) )
    //credentials are not valid -> redirect

//if credentials are valid, get the user that made last attempt
$user = Auth:getLastAttemped();

//check the status   
if( $user->status != '99' )
{
    //status ok -> log in the user
    Auth::login( $user );

    //redirect wherever you want
}
else
{
    //status is wrong -> redirect to login     
}

【讨论】:

    猜你喜欢
    • 2019-09-05
    • 2017-07-27
    • 1970-01-01
    • 2020-04-24
    • 2016-09-15
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多