简体   繁体   English

Laravel 5.4检查用户是否处于活动状态,如果没有,则返回消息

[英]Laravel 5.4 check if user is active or not and respond with message if not

I have a custom login controller, and I need to check if user is active or not and if not respond with message like please active your account first. 我有一个自定义的登录控制器,我需要检查用户是否处于活动状态,如果没有响应,请先激活您的帐户。

Here is my controller 这是我的控制器

public function login( Request $request ) {
        // validate the form data
        $this->validate( $request, [
            'email'    => 'required|email',
            'password' => 'required|min:6',
        ] );
        // attempt to login the supplier in

        if ( Auth::guard( 'supplier' )->attempt(
            [
                'email'    => $request->email,
                'password' => $request->password,
            ], $request->remember ) ) {

            return redirect()->intended( route( 'supplier-dashboard' ) );
        }

        $errors = [ $this->username() => trans( 'auth.failed' ) ];

        if ( $request->expectsJson() ) {
            return response()->json( $errors, 422 );
        }

        return redirect()->back()
                         ->withInput( $request->only( 'email', 'remember' ) )
                         ->withErrors( $errors );
    }

I can just add active => 1 to the if ( Auth::guard( 'supplier' )->attempt( but this will respond with wrong username or password 我可以在if ( Auth::guard( 'supplier' )->attempt(但这会以错误的用户名或密码进行响应if ( Auth::guard( 'supplier' )->attempt(添加active => 1

but I want it to respond with 'Active your account first' 但我希望它回复“首先激活您的帐户”

any help appreciated. 任何帮助表示赞赏。

I am not entirely sure what you are trying to do, but it seems like you are just trying to customize the validation messages. 我不确定您要做什么,但是似乎您只是在尝试自定义验证消息。 In laravel 5.4 you can customize the error message that the user sees like here . 在laravel 5.4中,您可以自定义用户在此处看到的错误消息。

You may customize the error messages used by the form request by overriding the messages method. This method should return an array of attribute / rule pairs and their corresponding error messages:

You should be able to do something like 你应该能够做类似的事情

public function messages()
{
     return [
         'email.required' => 'Active your account first',
         'password.required'  => 'Active your account first',
     ];
}

But a better way to do this would be using Laravels flash messaging . 但是,更好的方法是使用Laravels Flash消息传递 Essentially you would set a key and message like this: 本质上,您将这样设置密钥和消息:

$request->session()->flash('message', 'Active your account first');

Then to display the message in your view you can simply do this: 然后,要在您的视图中显示消息,您只需执行以下操作:

@if (Session::has('message'))
    <p>{!! session('message') !!}</p>
@endif
...
    if ( Auth::guard( 'supplier' )->attempt(
        [
            'email'    => $request->email,
            'password' => $request->password,
        ], $request->remember ) ) {
        if(!Auth::user()->active) {
           Auth::logout();
           $errors = ['active' => 'Please activate your account'];
        } else {
           return redirect()->intended( route( 'supplier-dashboard' ) );
        }
    } else {
        $errors = [ $this->username() => trans( 'auth.failed' ) ];
    }
...

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

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