Search code examples
phpauthenticationlaravel-5laravel-5.1laravel-authorization

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


Pulling my hair out over what seems a simple task. I use the username instead of email in my AuthController using the following code:

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

This has worked fine for a long time (when I wasn't planning on users activating their accounts). However, now I need users to activate their accounts, I know I need to do additional checks on login before they can see their dashboard.

I have modified the postLogin() method below, to what I thought would be correct:

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(),
                ]);
}

But all I get is the following error:

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

What is it that i'm doing wrong?

Andy


Solution

  • Just one glitch that I see there.

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

    should be

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