Laravel Log In Error

Asked

Viewed 1,570 times

2

I’m having trouble logging in Laravel.

See, it does not log the user, but also does not return error:

auth.php

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

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

My Model Php client.

<?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() }}
  • In the database, the fields are respectively email and password in the client table.

  • Seeing it like this all right! he did not make mistakes ?

  • None, if I give a dd(Auth::Attempt($credentials,false)) simply returns me false... ;(

  • you generated the password correctly?

3 answers

1

I am suspecting that you are not recording the user password correctly. To generate the user password you need to use the 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’s a dot there on that line $model->password = Hash::make(Input::get('password')); that needs to be done password with this Hash.

Check that the password was generated like this, because if not in the Auth::attempt will always return false.

If that’s the problem, change your script putting Hash::make: in the password recording and then try to enter the login form again.

Solution:

Utilize Auth::loginUsingId(1), where 1 is the number that identifies the user in the user table and it will be authenticated. You must fetch the data from 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 other commands works the same!

References:

  • Then, the password is being recorded and at this point of the system it is recorded and authenticated without hash even. It is a system that has been developed for some time and it has no encryption in the password itself and I am migrating it slowly, but the password at this time can not authenticate with hash because a part of the system is done in Codeigniter.

  • 1

    So really I was right, but, the Windows there is a way for you to authenticate the user id, I made the edit with the @Ewertonmelo solution item, that’s how

  • 1

    I’ll test it here...

  • By this other method also did not roll... :(

  • @Ewertonmelo check your application and the login part because it worked with me in my application this command, same as the previous one!

  • @Ewertonmelo I think you’re missing something in your application or a detail... ! is to work must be your database or at the time of the search it becomes difficult for me to say!

  • I was doing a revision now, from auth.php to View. I made some changes because the system owner told me that the login should be done in another table, taking advantage of the cue, I already put the Hash::make() and to authenticate the Hash::check('password',Input::get('password')), but it doesn’t work anyway, it simply returns false. I know there is a command that I can see the query that has just been executed, do you remember what it is??? So it is easier to debug...

  • The command I found...

  • For example, if I view the query executed behind Auth::Attempt() it returns it to me: string 'select * from cliente_contactwhereemail = ? limit 1' (length=57)

  • Did not return the password field that is passed in the array credentials...

  • 1

    He has another kind of internal check, but I gave you a form that can be used normally

Show 6 more comments

0

Problem solved.

An important point, initially I tried to authenticate with decrypted password, it returned false, this occurs pq internally the method attempt() calls the hash check natively Hash::check() then, I would never be able to authenticate the user without an Laravel hash. So there is no need to do anything else, just have the hash created. @Harrypotter, you mentioned this. Thank you, but overall, the solution is simpler than I imagined.

  • Ewerton, so that you did a text is repetition of what I just said below?

-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');
            }
        }
    }

Accounts.php Model

public static $rulesLogin = array(
                'username'      => 'required|max:50',
                'password'      => 'required|min:6'
        );
  • Again: can you explain why your code solves the problem? Check out the guide [Answer].

Browser other questions tagged

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