Authentication Laravel 5.2

Asked

Viewed 1,303 times

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.phpI 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?

  • Yes, I’m using the eloquent..

  • By default, Laravel (including 5.2) redirects authenticated users to /. The route candidato/perfil (as well as the others) is configured in AuthController?

  • Your App Login is a model?

  • @Miguel, yes it is a model, it is the class User.php that comes when you lower the Laravel.

  • @Eduardoalmeida, the problem is Auth::attempt() it always returns false, even if the user is in the database...

  • @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');

  • @Miguelbatista, not yet encrypted the password, leaves it "raw" to make the tests...

  • @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);

  • @Miguelbatista, humm.. interresante not Tab of this detail.. I will test here...

  • for displaying Auth::logout();

  • @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...

  • 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.

Show 8 more comments

1 answer

3


To authenticate using the Laravel method attempt() you must certify that you have encrypted the password with the Hashing of the Laravel 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 make the user query using the model as 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'); 
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.