Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CleytonBonamigo's avatar

Auth::attempt always return false

Hello everybody, I'm kind a newbie on Laravel and start learning L 5 form the beggining.

Sure that I'm doing a really really noob error, but I don't figure out.

Well, I'm trying to make a custom login, but, the Auth::attempt() always return false. Here's my code.

Config\auth.php

<?php

return [

    'driver' => 'eloquent',

    'model' => 'App\User',

    'table' => 'usuarios',

    'password' => [
        'email' => 'emails.password',
        'table' => 'password_resets',
        'expire' => 60,
    ],

];

Migration & Seed

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('usuarios', function(Blueprint $table)
        {
            $table->increments('usu_id');
            $table->string('usu_nome')->unique();
            $table->string('usu_senha', 60);
            $table->integer('clu_id');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('usuarios');
    }

}
<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\User as User; // to use Eloquent Model 

use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        $this->call('UserTableSeeder');
    }

}

class UserTableSeeder extends Seeder {
    public function run() {
        // add 1st row
        User::create(['usu_nome' => 'UsuTeste', 'usu_senha' => Hash::make('123456')]);
    }
}

My Model in App\User.php

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'usuarios';
    protected $primaryKey = 'usu_id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['usu_nome', 'usu_senha'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['usu_senha', 'remember_token'];

}

And finally, my custom auth controller

<?php namespace App\Http\Controllers;

use Auth;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Hash;

class AuthController extends Controller {

    public function getLogin(){
        return view('auth.login');
    }

    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate(Request $request){
        $credentials = array('usu_nome' => $request->usu_nome, 'usu_senha' => $request->usu_senha);

        if (Auth::attempt($credentials, false)){
            dd('success');
            return redirect()->intended('dashboard');
        }else{
            dd('failed');
        }
    }
}
0 likes
12 replies
stefanwegner's avatar

I guess the problem is the validation of the credentials in Illuminate\Auth\EloquentUserProvider - it seems that laravel can't hash your password field and returns false ... test it with naming the password field "password"

RomainLanz's avatar

You need to force an encrypted password on your User model.

/**
 * Password need to be all time encrypted.
 *
 * @param string $password
 */
public function setPasswordAttribute($password)
{
    $this->attributes['password'] = bcrypt($password);
}
2 likes
CleytonBonamigo's avatar

@RomainLanz Try to do that, but, nothing happens. I look at Acessors & Mutators documentation, change from setPasswordAttribute to setUsuSenhaAttribute and still the same, failed login.

RomainLanz's avatar
Level 14

Try to rename your col to password.

/**
 * Retrieve a user by the given credentials.
 *
 * @param  array  $credentials
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 */
public function retrieveByCredentials(array $credentials)
{
    // First we will add each credential element to the query as a where clause.
    // Then we can execute the query and, if we found a user, return it in a
    // Eloquent User "model" that will be utilized by the Guard instances.
    $query = $this->createModel()->newQuery();

    foreach ($credentials as $key => $value)
    {
        if ( ! str_contains($key, 'password')) $query->where($key, $value);
    }

    return $query->first();
}
CleytonBonamigo's avatar

@RomainLanz Thanks buddy, it worked! Just by curiosity, there's a way to change col from password to another one, like 'usu_senha'?

hafizabi's avatar

I had this issue in https://laracasts.com/series/laravel-from-scratch-2017 course. And fixed it by adding

public function setPasswordAttribute($value)
    {
        if( \Hash::needsRehash($value) ) {
            $value = \Hash::make($value);
        }
        $this->attributes['password'] = $value;
    }

in User.php (User Model)

The issue is password was not begin hashed so it should be hashed before saving it DB. May be there is a nice way.

1 like
granttrudel's avatar

@hafizabi Same here, thanks for the help! I put the above code in /vendor/laravel/framework/src/Illuminate/Auth/Authenticatable.php rather than the User model since I think it ought to be in the parent class.

Alternatively, as per the comments under this that Laracast: https://laracasts.com/series/laravel-from-scratch-2017/episodes/19 you can change RegistrationController to encrypt the password from the request before it is put into the database:

$user = User::create([ 
    'name' => request('name'),
    'email' => request('email'),
    'password' => bcrypt(request('password'))
]);

@support Please update the Laracast (at about 4:58)?

1 like

Please or to participate in this conversation.