Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel's Auth::attempt() returns true but Auth::check() returns false

As the title says, Laravel's function Auth::attempt() returns true in the following code section (with unimportant parts deleted):

public function doLogin()
{
    $validator = [..]

    if ($validator->fails()) {
        [..]
    } else {
        $userdata = array(
            'username'  => Input::get('username'),
            'password'  => Input::get('password')
        );

        if (Auth::attempt($userdata, true)) {
            return Redirect::to('/');
        } else {        
            return Redirect::to('login');
        }
    }
}

But when we are redirected, we try to check if an user is really logged in with Auth::check(), and it returns false somehow.

We have tried every possible solution on Google and did not succeed with any of it. For example we added a remember_token but it did not change anything, though it proved us that the Auth::attempt() does something because the remember_token is set in the database.

As a last resort, we even tried to print something in the Auth::attempt() method of Laravel in ./vendor/laravel/framework/src/Illuminate/Auth/Guard.php but we did not see anything of that, not even with print_r. We tried to find other attempt-functions in the complete codebase but none is found.

It could be that something about changing User to an translated form of it makes it broken, but then the function Auth::attempt() should also be broken, probably.

It seems that something really magical happens but we have no idea what or how. Does anybody else have an idea?

like image 644
Renzeee Avatar asked Mar 17 '23 11:03

Renzeee


1 Answers

By default, Laravel assumes that each table has a primary key called id.

Possible solution is to do this in your model:

protected $primaryKey = 'id'; //Or what ever id name do you have.

This error may occur if you are trying to use laravel-mongodb with custom increment and unique generated id rather than the default _id from MongoDB. The solution in this case is not to name your custom id like this 'id' because it will make Laravel confused. However, name it anything else.

like image 63
abdawoud Avatar answered Mar 20 '23 05:03

abdawoud