2
votes

I'm developing an app in Laravel 5.1. I need to be able to login users only with e-mail (not using password). I have a password field in my login form because I need it for other reasons, but the only thing that I need to check is that the user email exists in my users table. I rewrited the AuthController, and I'm trying to use the attempt() method, but when i try to login with a valid e-mail, it gives me the following error:

ErrorException in EloquentUserProvider.php line 112: Undefined index: password

How can I tell to EloquentUserProvider that I'm not using password to login, only username (e-mail)?

Thank you!!

3

3 Answers

5
votes

you can find the email with queries and have the id of user and then

Auth::loginUsingId(user_id);

it will authenticate the user_id .

3
votes

If you're not using a password, really all you need to do is:

$authorised = User::where('email', $email)->exists();

But, if you want to create your own passwordless UserProvider, you could extend EloquentUserProvider and override the validateCredentials to not check the password, as so:

use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Auth\EloquentUserProvider;

class NoPasswordUserProvider extends EloquentUserProvider {


public function __construct(HasherContract $hasher, $model)
{
    parent::__construct($hasher, $model);
}

/**
 * Validate a user against the given credentials.
 *
 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
 * @param  array  $credentials
 * @return bool
 */
public function validateCredentials(UserContract $user, array $credentials)
{
    return true;
}

}

You'll probably need to create new service provider for it, too:

class NoPasswordUserServiceProvider extends ServiceProvider {

public function boot()
{
    Auth::extend('NoPasswordAuth', function($app)
    {
        $model = $this->app['config']['auth.model'];
        return new NoPasswordUserProvider($this->app['hash'], $model);
    });
}

/**
 * Register the service provider.
 *
 * @return void
 */
public function register()
{
    // TODO: Implement register() method.
}
}

Obviously, the code isn't fully tested but that should more or less work.

1
votes

What I finally did was to write my own Auth Controller. I also writed my own middleware. I did like this because I needed to do more actions when user logs in. I suppose the other answers were OK but as I needed other functionality while user login I decided to write my own controller to implement the functions I need. In my Auth controller I wrote my own login function, my own logout and so on.... Thank you so much for the answers!