Laravel 5.2在标准Auth控制器

时间:2017-11-17 06:43:38

标签: laravel authentication laravel-5.2

我正在尝试为laravel标准auth添加一个条件。我找到了很多关于这个问题的论坛。他们中的大多数都在供应商文件夹中进行更改,这是我不想做的。我还发现了一种在AuthController.php中添加凭证(Request $ request)功能的方法,但我没有运气。它看起来像这样:

protected function credentials(Request $request)
{
    return array_merge($request->only($this->username(), 'password'),
        ['Type' => 1]);

}

有没有人解决这个问题或者可以建议我该怎么做?感谢

1 个答案:

答案 0 :(得分:1)

以下是您可以在AuthController中使用的功能来覆盖登录功能:

public function login(Request $request)
    {
        $validator = Validator::make(
            $request->all(),
            array(
                'user_name' => 'required',
                'password' => 'required',
            ),
            array(
            )
        );

        if($validator->fails()){
            return redirect()->back()->withInput()->withErrors($validator->errors(),'invalid_credentials');
        }

        if (!\Auth::validate(['user_name' => $request->user_name, 'password' => $request->password])) {


            return redirect()->back()->withInput($request->only('user_name'))->withErrors([
                'user_name' => 'Incorrect Username or Password',
            ],'invalid_credentials');
        }

        $credentials  = array('user_name' => $request->user_name, 'password' => $request->password);

        if (\Auth::attempt($credentials, true)){

            /* Check your user type condition */
            if(\Auth::User()->type == '1'){
                return redirect()->intended($this->redirectPath());
            }
            else{
                \Auth::logout();
                return redirect()->back()->withInput($request->only('user_name'))->withErrors([
                    'user_name' => 'Your type validation message',
                ],'invalid_credentials');
            }
        }

        return redirect()->back()->withInput($request->only('user_name'))->withErrors([
            'user_name' => 'Incorrect email address or password',
        ],'invalid_credentials');
    }

希望这可以帮助你.. :)