2

In the manual I can read:

By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController

However in my case I would like to use the unique_id field which is provided by my company to authenticate my user. This field is not a username so I feel bad about using the username() method to get this. Is there another way to tell Laravel Authentication to use something else than email to authentify my users?

7
  • You mean for the make:auth? Commented Apr 2, 2020 at 17:59
  • Yes the default one Commented Apr 2, 2020 at 17:59
  • You still define the username() method, you just make it return unique_id instead of username. That method simply tells the auth what column to use if you don't plan on using the default. Commented Apr 2, 2020 at 18:00
  • Yeah, I agree that the username() method's name is quite unfortunate, but that's the way to go. Commented Apr 2, 2020 at 18:01
  • But still if I manually call username I will get a confused result which is not the username but an id. Commented Apr 2, 2020 at 18:01

3 Answers 3

2

You can override validateLogin and credentials methods in your Auth/LoginController

protected function validateLogin(Request $request)
{
    $request->validate([
        'unique_id' => 'required',
        'password' => 'required|string',
    ]);
}

protected function credentials(Request $request)
{
    return array_merge($request->only('unique_id', 'password'));
}
Sign up to request clarification or add additional context in comments.

1 Comment

You must override username() method too.
1

Ignore the fact that the method is called username() and go for it.

Comments

-2

You can overwrite username() method in App\Http\Controllers\Auth\LoginController::class like this:

/**
* Get the login username to be used by the controller
* You Username will be User $unique_id
*/
public function username()
{
    return 'unique_id'
}

Comments

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.