0

i have laravel project.I want to use username authentication.Is there any way to define which attribute in table that can be used for username authentication?

I read some discussion on the internet.People says we should use this method for username authentication in logincontroller.php

public function username() { return 'username' }

what is this method trying to return?

  • Did you actually try the code snippet above? Did you add the `username` column to your database table? If so, what errors are you getting? – Mike Apr 16 '19 at 03:06
  • i did,i have database that have 'user_name' attribute.I change the return from 'return username'to 'return user_name'.My login page doesn't show any error but it doesn't redirect to the next page everytime i try to login.It always stay on the login page.But my point is what does the method do?what is the possible way to define which attribute in the table database that can be used as a username in laravel auth – galih indra Apr 16 '19 at 10:29

1 Answers1

0

It's used to validate the login and query in your database.

// trait AuthenticatesUsers

/**
* Validate the user login request.
*
* @param  \Illuminate\Http\Request  $request
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function validateLogin(Request $request)
{
    $request->validate([
        $this->username() => 'required|string',
        'password' => 'required|string',
    ]);
}

/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
    return 'email';
}

Check the API Reference, and some answers by others.

Cloud Soh Jun Fu
  • 1,456
  • 9
  • 12