Laravel 5.2 在标准 Auth 控制器中添加了一项登录条件

问题描述 投票:0回答:1

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

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

}

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

php laravel authentication laravel-5
1个回答
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');
    }

希望这对你有帮助..:)

© www.soinside.com 2019 - 2024. All rights reserved.