Laravel Auth :: Atempt () always returns false

3

I tried to authenticate, but always returns false in the Auth::attempt() method of Laravel:

$prontuario = Input::get('prontuario');
$senha = Input::get('senhas');  
if (Auth::attempt(['prontuario' => $prontuario, 'senha' => $senha]))
{ 
    return Redirect::to('/perfil');
}
else
{
    return Redirect::to('/')->withErrors('Prontuario ou senha inválidos!');
}

The passwords entered in the database are already hashed, but still returns false in if condition and I can not login. What's missing to work?

    
asked by anonymous 03.11.2016 / 00:50

1 answer

5

The default way to do authentication follows a email and password users : email and password ), as demonstrated link and this other link .

The ideal and default code would be :

if (Auth::attempt(['email' => $email, 'password' => $password])) {
     // Authentication passed...
     return redirect()->intended('dashboard');
}

Outside of this pattern you can also an instance of class in> User :

$user = User::find(1);
if ($user)  // encontrou o usuário
{
    Auth::login($user); // autentica o usuário
}

Do the user search by bringing an instance of class User and step in method Auth::login($user) and also / em> .

In your specific case I noticed that it does not follow a pattern, so you should use the instance form:

$prontuario = Input::get('prontuario');
$senha = Input::get('senhas');  

$user = User::where('prontuario', $prontuario)
            ->first(); // buscando um registro desse pontuário

if ($user) // encontrou o usuário
{
   if (Hash::check($senha, $user->senha)) // conferindo senha
   {
       Auth::login($user); // autentica o usuário
   }
   else
   {
       //senha inválida...
   }
}
else
{
   return Redirect::to('/')->withErrors('Prontuario ou senha inválidos!');
}   

There is also a factor, follows a pattern from a Users table and a class from User , if there was a change in this you should take care to change everywhere so your code does not break, ie it is possible to change the table and model for authentication, but this code should reflect in the settings of the and this will depend on the version.

    
03.11.2016 / 02:09