3

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

2
  • I don't think the problem starts in this piece of code. Can you post your AuthController ? Commented Feb 8, 2016 at 19:15
  • Without this piece of code it's fine Commented Feb 8, 2016 at 19:18

2 Answers 2

4

Just one glitch that I see there.

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

should be

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

Sign up to request clarification or add additional context in comments.

3 Comments

Well that worked, how comes I need to change this though?
I just trust documentation and follow their code. I have no time and expertise to judge laravel's designers. :)
I meant and explanation as to why it needed to change, not to judge Laravel lol. Usually answers have an explanation as to why you need to do the changes
0

In Laravel 5.1, the $auth attribute does not exist in the AuthController class constructor, as explained in the 5.1 upgrade guide. So you're referencing a property which does not exist.

1 Comment

Indeed, but this was a year ago and to be fair, was my first Laravel project (didn't upgrade from Laravel 5, went straight in at 5.1) so at the time the accepted answer is what helped. Thank you for the explanation though, hopefully someone will benefit from it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.