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"
Mar 25, 2015
12
Level 1
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');
}
}
}
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();
}
Please or to participate in this conversation.