Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Auth::attempt using either email or username and password

In laravel for login attempt I generally use something like this:

if (Auth::attempt(array('email' => $usernameinput, 'password' => $password), true))
{
    // The user is being remembered...
}

Basically $usernameinput will check with email from my table.

I was thinking to do it in a different way, like there is email, username and password in my table. $usernameinput can be either email or username field in my table.

How can I Auth::attempt with a condition like:

(email==$usernameinput OR username==$usernameinput) AND password == $password
like image 831
Chinmoy Avatar asked Feb 11 '14 06:02

Chinmoy


People also ask

What is attempt () in Laravel?

The attempt method accepts an array of key / value pairs as its first argument. The password value will be hashed. The other values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column.

What is auth ()- user () in Laravel?

Auth::user() — You can check if a user is authenticated or not via this method from the Auth Facade. It returns true if a user is logged-in and false if a user is not. Check here for more about how Facades work in Laravel.

What is Auth :: Routes () in Laravel?

Auth::routes() is just a helper class that helps you generate all the routes required for user authentication. You can browse the code here https://github.com/laravel/framework/blob/5.3/src/Illuminate/Routing/Router.php instead.


3 Answers

Well, you could simply check if $usernameinput matches an email pattern and if it does, use the email field, else use the username field. This sounds reasonable because, well, you really shouldn't have any e-mail in your database that doesn't match the pattern. Something like this:

$field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

if (Auth::attempt([$field => $usernameinput, 'password' => $password], true)) {
    // ...
}

The other option is to extend Illuminate\Auth\Guard adding the wanted functionality and setting it as the auth component, in a new service provider.

like image 75
rmobis Avatar answered Sep 30 '22 19:09

rmobis


It can be done something like this

$field = Validator::make(array('email' => $usernameinput, array('email' => 'email'))->passes() ? 'email' : 'username';
if (Auth::attempt(array($field => $usernameinput, 'password' => $password), true)) {
    return Redirect::intended('profile');
}
like image 45
ssi-anik Avatar answered Sep 30 '22 18:09

ssi-anik


Users could use an email field as their username, and it might not match the email they used for the email field. Safer to attempt authentication twice:

if (Auth::attempt(array(
            'email'     => Input::get('email_or_username'),
            'password'  => Input::get('password')
        )) ||
    Auth::attempt(array(
            'username'     => Input::get('email_or_username'),
            'password'  => Input::get('password')
        ))) {
    // SUCCESS
} else {
    // FAILURE
}
like image 21
patsplat Avatar answered Sep 30 '22 19:09

patsplat