Laravel 5.1 - 验证用户名、密码和(如果确认)= 1


Laravel 5.1 - Authenticate Username, Password AND if confirmed = 1

把我的头发,这似乎很简单。我使用以下代码在AuthController中使用username而不是email

/**
 * Set the login system to use username instead of email address
 * @var string
 */
protected $username = 'username';

这已经工作了很长时间(当我不打算让用户激活他们的帐户时(。但是,现在我需要用户激活他们的帐户,我知道我需要在登录时进行额外的检查,然后才能看到他们的仪表板。

我已经修改了下面的postLogin()方法,我认为是正确的:

public function postLogin(Request $request)
{
    $this->validate($request, [
        'username' => 'required', 'password' => 'required',
    ]);
    $credentials = $request->only('username', 'password');
    $credentials = array_add($credentials, 'confirmed', '1');
    if ($this->auth->attempt($credentials, $request->has('remember')))
    {
        return redirect()->intended($this->redirectPath());
    }
    return redirect($this->loginPath())
                ->withInput($request->only('username', 'remember'))
                ->withErrors([
                    'username' => $this->getFailedLoginMessage(),
                ]);
}

但我得到的只是以下错误:

Undefined property: App'Http'Controllers'Auth'AuthController::$auth

我做错了什么?

安 迪

我在那里看到的只是一个小故障。

if ($this->auth->attempt($credentials, $request->has('remember')))

应该是

if (Auth::attempt($credentials, $request->has('remember')))

在 Laravel 5.1 中,

$auth 属性在 AuthController 类构造函数中不存在,如 5.1 升级指南中所述。因此,您引用的是一个不存在的属性。