Question:
I'm trying to login to laravel 5.2 but it's not authenticating the user in the table.
in the auth.php
file I changed the validation table for 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 in the path Http/Controllers/LoginController.php
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();
}
}
}
But it always falls into else
, even with the user in the bank, it does not validate.
o dd(Auth::attempt($credenciais));
is always returning false
.
Does anyone know why he doesn't authenticate??
Answer:
To authenticate through Laravel using the attempt()
method you must certify that you have encrypted the password with laravel hashing as follows:
Hash::make('sua senha')
in the database or
bcrypt('sua senha')
as specified here .
To validate the password
If you don't want to use attempt()
for other reasons,
you should query the user using the model, for example:
$usuario = Usuario::whereEmail($request->get('email'))->first();
ou
$usuario = Usuario::where('email','=',$request->get('email'))->first();
if ($usuario && Hash::check($request->get('senha'), $usuario->senha)) {
Auth::login($usuario);
return redirect('sua página após logar');
} else {
return redirect('login');
}