如何在登录 Laravel 5 时检查用户状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39756506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:32:02  来源:igfitidea点击:

How to check user status while login in Laravel 5?

laravelauthenticationmiddlewarelaravel-middleware

提问by Zaman

I have used Laravel Authentication (Quickstart). But I need to check the status of the user (approved/pending). If not approved, then an error will be shown in the login page. I need to know in which file I have to make the change and what is the change. Currently I am working on Laravel 5.3.

我使用过 Laravel 身份验证(快速入门)。但我需要检查用户的状态(已批准/待定)。如果未获批准,则登录页面将显示错误。我需要知道我必须在哪个文件中进行更改以及更改内容。目前我正在研究 Laravel 5.3。

回答by Camilo Rojas

You can create a Laravel Middlewarecheck the link for additional info

您可以创建Laravel Middleware检查链接以获取更多信息

php artisan make:middleware CheckStatus

modify your middleware to get

修改你的中间件以获得

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class CheckStatus
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        //If the status is not approved redirect to login 
        if(Auth::check() && Auth::user()->status_field != 'approved'){
            Auth::logout();
            return redirect('/login')->with('erro_login', 'Your error text');
        }
        return $response;
    }
}

then add your middleware to your Kernel.php

然后将您的中间件添加到您的 Kernel.php

'checkstatus' => \App\Http\Middleware\CheckStatus::class,

and finally add the middleware to your route

最后将中间件添加到您的路线中

Route::post('/login', [
    'uses'          => 'Auth\AuthController@login',
    'middleware'    => 'checkstatus',
]);

I hope it helps

我希望它有帮助

回答by saad siddiqui

upper answer saves me

上面的答案救了我

  if (Auth::attempt(['email'=>$input['email'],'password'=>$input['password'], 'user_status'=>1 ]))

this will check the status

这将检查状态

回答by lightWay

I don't agree with upper answer, which will lead to your application performance is very low, and also don't recommend to modify the Laravel's source code.

我不同意楼上的回答,这会导致你的应用性能很低,也不建议修改Laravel的源代码。

So you can rewrite getCredentials function to your app\Http\Controllers\Auth\AuthController.php file like this:

所以你可以像这样将 getCredentials 函数重写到你的 app\Http\Controllers\Auth\AuthController.php 文件中:

<?php
//app\Http\Controllers\Auth\AuthController.php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;

class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    //you need add this function
    protected function getCredentials(Request $request)
    {
        $data = $request->only($this->loginUsername(), 'password');
        $data['is_approved'] = 1;
        return $data;
    }
}

then you can use Laravel Authentication (Quickstart) directly.

那么您可以直接使用 Laravel 身份验证(快速入门)。

Hope this will help.

希望这会有所帮助。

回答by Aaron Nu?ez

I found a simple solution for this. Artisan create App\Http\Controllers\Auth\LoginController, in this default controller just add this code if you have some conditions to login, for example I have a field state, you posibbly have status, email_statusor other.

我为此找到了一个简单的解决方案。Artisan create App\Http\Controllers\Auth\LoginController,在这个默认控制器中,如果您有一些登录条件,只需添加此代码,例如我有一个字段state,您可能有status, email_status或其他。

// Custom code for Auth process
protected function credentials( Request $request )
{
    $credentials = $request->only($this->username(), 'password');

    $credentials['state'] = 1;

    return $credentials;

}

回答by Riccardo Mel

The pinned answer is the best approach.

固定答案是最好的方法。

Just a note: if you are using Laravel 5.8+you need use:

请注意:如果您使用的是Laravel 5.8+,则需要使用:

//Default Auth routes
Auth::routes();

//Override and add middleware 
Route::post('/login', [
'uses'          => 'Auth\LoginController@login',
'middleware'    => 'checkstatus',
]);

回答by Vikash

Follow the steps...

按照步骤...

First add a column in your user table (suppose is_approved)

首先在您的用户表中添加一列(假设 is_approved)

In App/Http/Controllers/Auth/LoginController file

在 App/Http/Controllers/Auth/LoginController 文件中

public function authenticate()
{
    if (Auth::attempt(['email' => $email, 'password' => $password, 'is_approved'=>1])) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
}

Hope this will help

希望这会有所帮助