laravel Authentication 5.2

5

I'm trying to log in to laravel 5.2 but is not authenticating the user to the table.

In the file auth.php I changed the validation table to login :

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'login',
        ],

And I've added 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 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();
        }
    } 
}

But it always falls into else , even though it takes the user to the bank, it does not validate.

dd(Auth::attempt($credenciais)); is always returning false .

Would anyone know why it does not authenticate?

    
asked by anonymous 19.05.2016 / 04:24

1 answer

3

To authenticate by Laravel using the attempt() method you must make sure that you have encrypted the password with the laravel Hashing as follows:

Hash::make('sua senha')

in the database or

bcrypt('sua senha')

as specified here .

To validate the password

If you do not 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'); 
}
    
19.05.2016 / 18:32