5
I am trying to log in to Laravel 5.2 but it is not authenticating the user in the table.
in the archive auth.php
I changed the validation table to login
:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'login',
],
And I set the Login class:
'providers' => [
'login' => [
'driver' => 'eloquent',
'model' => App\Login::class,
],
I created my Model in App\Login
:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Login extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
And on the way Http/Controllers/LoginController.php
I created the controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
class LoginController extends BaseController
{
public function logar(Request $req) {
$validator = Validator($req->all(), [
'email' => 'required',
'password' => 'required'
]);
if($validator->fails()) {
return redirect('candidato/login')
->withErrors($validator)
->withInput();
}
$credenciais = ['email' => $req->input('email'), 'password' => $req->input('password')];
dd(Auth::attempt($credenciais));
if(Auth::attempt($credenciais, true)) {
return redirect('candidato/perfil');
} else {
return redirect('candidato/login')
->withErrors(['errors' => 'login inválido'])
->withInput();
}
}
}
Only he always falls in else
, even having the user in the bank, it does not validate.
the dd(Auth::attempt($credenciais));
is always returning false
.
Would anyone know why he didn’t authenticate??
Question: Are you using
Eloquent
even to authenticate users?– StillBuggin
Yes, I’m using the eloquent..
– MeuChapeu
By default, Laravel (including 5.2) redirects authenticated users to
/
. The routecandidato/perfil
(as well as the others) is configured inAuthController
?– StillBuggin
Your App Login is a model?
– Miguel
@Miguel, yes it is a model, it is the class
User.php
that comes when you lower the Laravel.– MeuChapeu
@Eduardoalmeida, the problem is
Auth::attempt()
it always returns false, even if the user is in the database...– MeuChapeu
@Meuchapeu You encrypted the password with the Windows hash in the bank? if using another type of encryption it will not authenticate. example $password = Hash::make('123456');
– Miguel Batista
@Miguelbatista, not yet encrypted the password, leaves it "raw" to make the tests...
– MeuChapeu
@Meuchapeu if you do not encrypt Attempt does not authenticate. So it would only be possible if you query with the model, get the object and Auth::login($objectUsuario);
– Miguel Batista
@Miguelbatista, humm.. interresante not Tab of this detail.. I will test here...
– MeuChapeu
for displaying Auth::logout();
– Miguel Batista
@Miguelbatista, I would have to do a Hash::make('password') there to validate? because it’s not working, I’m encrypting at the time of registration, and still won’t.. and in $req->input('password') tbm would have to put the Hash? Right? That’s what I did and nothing works...
– MeuChapeu
To validate you should not use hash::make() because you would be creating another encryption, take a look at https://laravel.com/docs/5.0/hashing you use hash::check('password', $passwordDoBanco); to compare the password.
– Miguel Batista