How would I let users log in using either their username or email from a single input field in Laravel 5?
Using a Request, the data gets validated upon method injection. In L4, I would detect if the input matched the regex of an email and then manually set it as the field to be validated. How would something like this be accomplished in L5?
Thank you. Okay, so I create a new class extended from the base one and use this as my instance? Sorry, it my question is stupid, but I'm a very new beginner ;-) Does the core of Laravel will be easier translateable in the future? Why does the core doesn't make use of the language folder and the @lang-feature?
For message there is now a function in AuthenticatesAndRegistersUsers trait :
/**
* Get the failed login message.
*
* @return string
*/
protected function getFailedLoginMesssage()
{
return 'These credentials do not match our records.';
}
To log in with username instead of email, just add:
protected $username = 'username';
to AuthController.php
where 'username' is the field in your users table.
However, this will replace email with username. To log in with either email or username, (as far as I know) you'll need to override the postLogin() method with your own authentication logic in AuthController.php.
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function loginUsername()
{
return 'login';
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function getCredentials(Request $request)
{
$login = $request->get('login');
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
return [
$field => $login,
'password' => $request->get('password'),
];
}
As for me this looks cleaner.
Another thing I didn't like in laravel is that there is no way to use Validator functions independently, for example in Kohana I could use the same functions from Validation module just like this if (Valid::email('test@test.com')) ... Btw, Kohana supported login by both email and username out of the box.
@vedmant I've found this thread searching about the same problem. Sorry I'm quite a noob. Overloading the to functions means that this code is put in the AuthController instead on the trait?
@betog Yes, right, you can find this method in AuthenticatesUsers trait but you overload this method with method in your AuthController, cause AuthenticatesUsers is part of Laravel framework, but AuthController if part of your application.
However, what about handling password resets, etc? I see that there is a contract called UserProvider, here: Illuminate\Contracts\Auth\UserProvider. Maybe there is a smart way to change the approach once, and it will be reflected everywhere.
The accepted answer on this discussion looks like it completely bypasses the login throttling that comes for free. It would be nice to see an updated solution to this problem.
@bertog - Yeah place that code in the the AuthController. You'll be overriding the methods like that.
I tested this for my app recently, and it works a charm. You do need to remember to call the field in your login form "login" if you want to use the above example directly.
We can write the where clause with callbacks to check wheather user exist on their username or email with their passwords.
Best solution i found from :
This works fine for me: In Laravel 5.4
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(found in app\Http\Controllers\Auth):
public function username()
{
return 'username';
}
Then, set your input type to text in your form/blade - for instance:
@Bagmaz, It didn't work on mine. I did some research and I found this working on the latest version of laravel. I only tried the name of the user since I'm still studying laravel. My future plan uses this method on all users(admin, guest, superadmin). I tried to put the function inside the controller I guess it the base controller that inherits all the controller on different users. But there's no luck at all.