简体   繁体   English

如何在Laravel中使用自定义条件进行身份验证?

[英]How can I Authenticate with custom condition in Laravel?

In Laravel documentation there is simple Authentication with condition 在Laravel文档中,有简单的Authentication with condition

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

In my app User can have one of few statuses. 在我的应用中,用户可以拥有少数几种状态。 I want to authenticate AGAINST one of those: status != '99'. 我想验证其中一个:status!='99'。

Is there a way to achieve this within this request or some extra logic is required before Auth attempt is fired? 有没有办法在此请求中实现此功能,或者在Auth尝试被触发之前需要一些额外的逻辑?

Checking for a != condition with the Auth::Attempt method is impossibile, unless you override the EloquentServiceProvider::retrieveByCredentials method. 除非重写EloquentServiceProvider::retrieveByCredentials方法,否则使用Auth::Attempt方法检查!=条件是不可能的。

You could check the User status during the login process and authenticate only if the the status is different from 99 . 您可以在登录过程中检查User状态,并且仅在状态不同于99身份验证。

In your check login method: 在您的支票登录方法中:

//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     
}

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

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