Login Error in Laravel

2

I'm having trouble logging in to the user in Laravel.

Look, it does not log in to the user, but it also does not return an error:

auth.php

return array(
    'driver' => 'eloquent',
    'model' => 'Cliente',
    'table' => 'cliente',

    'reminder' => array(
        'email' => 'emails.auth.reminder',
        'table' => 'password_reminders',
        'expire' => 60,
    ),
);

My Model Client.php

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Cliente extends Eloquent implements UserInterface, RemindableInterface {
    use UserTrait, RemindableTrait;

    protected $table = 'cliente';
    protected $hidden   = array('password', 'remember_token');
    protected $fillable = [/*Meus campos*/];

    public function getAuthIdentifier(){
        return $this->getKey();
    }

    public function getAuthPassword(){
        return $this->password;
    }
}

My Controller AuthController.php

class AuthController extends BaseController{

    public function __construct(){}
    public function postLogin(){
        $credentials = [
            'email'    => Input::get('email'),
            'password' => Input::get('password')
        ];

        if(Auth::attempt($credentials,false)){
            return Redirect::to('/reserva');
        }

        return Redirect::to('/')
            ->with('message','Erro ao se logar, verifique o e-mail ou senha digitado.');
    }

    public function getLogout(){
        Auth::logout();
        return Redirect::to('login');
    }
}

My view:

{{ Form::open(['url' => 'auth/login','method' => 'post']) }}
    @if(Session::has('message'))
        <div class="alert alert-info">{{ Session::get('message') }}</div>
    @endif

    {{ Form::text('email'); }}  
    {{ Form::password('password'); }}   
    {{ Form::submit('Acessar') }}
{{ Form::close() }}
    
asked by anonymous 09.06.2014 / 00:19

3 answers

1

I suspect that you are not writing the user's password correctly. To generate the user password you need to use Hash: make .

Obs: Código abaixo é simplesmente para gravar o cadastro de usuários
public function save() {
    if ((int) Input::get('id', 0) > 0) {
        $model = $this->repository->get(Input::get('id')); 
        if (((int)Input::get('password',0)) != 0){
            $model->password = Hash::make(Input::get('password'));
        }
    } else {
        $model = $this->repository->create();
        $model->password = Hash::make(Input::get('password'));
    }

    $model->admin    = Input::get('admin');
    $model->email    = Input::get('email');
    $model->username = Input::get('username');
    $model->namefull = Input::get('namefull');
    $model->active   = Input::get('active');
    $model->filialid = Input::get('filialid');

    if (ModelState::valid($model)) {
        $model->save();
        return Redirect::route('admin.user.update', array($model->id));
    } else {
        return Redirect::route('admin.user.update', array(Input::get('id', NULL)))->withErrors(ModelState::errors());
    }
}

There is a point there in this line $model->password = Hash::make(Input::get('password')); that the password needs to be done with this Hash . p>

Make sure the password was generated like this, because if not in Auth::attempt it will always return false.

If this is the problem, change your script by placing Hash :: make in the password recording and then try to log in again via the login form .

Solution:

Use Auth :: loginUsingId (1) , where 1 is the number that identifies the user in the user table and with that it will be authenticated. You should search for the data by Eloquent , example:

$cliente = Cliente::where('email', '=', $email)
                    ->where('password','=', $password)
                    ->first();
if ($cliente){
  //logado
  Auth::loginUsingId($cliente->id);
} else {
  // não foi encontrado,
}

With this all the other commands works the same way!

References:

09.06.2014 / 03:09
0

Problem solved.

One important point, initially I tried to authenticate with the decrypted password, it returned false, this occurs because internally the attempt() method natively calls the hash check Hash::check() so I would never be able to authenticate the user without a Laravel hash . With this there is no need to do anything else, just have the hash created. @HarryPotter, you mentioned that. Thanks, but overall, the solution is simpler than I imagined.

    
09.06.2014 / 20:45
-1

ContasController.php

public function postLogin(){
        $validator = Validator::make(input::all(), User::$rulesLogin);
        if( $validator->fails() ){
            return Redirect::route( 'home' )
                ->withErrors( $validator );
        } else {
            if(Auth::attempt(array(
                'username'     => Input::get('username'),
                'password'     => Input::get('password'),
                'active'    => 1
            ))){
                return Redirect::route('home');
            } else {
                return Redirect::route( 'home' )->with('global', 'conta/senha não confere');
            }
        }
    }

Contas.php Model

public static $rulesLogin = array(
                'username'      => 'required|max:50',
                'password'      => 'required|min:6'
        );
    
13.06.2014 / 11:52