brianrhea
brianrhea

Reputation: 3724

Laravel 4 : User not staying Logged In

My user authentication broke after I upgraded Laravel to 4.1 from 4.0.

I followed the Upgrade Guide provided by the documentation.

Now, when I login, it seems only to last for one request.

For example, in my HomeController index method, I force login like so:

Auth::login(User::find(1), true);
$user = Auth::user();
return View::make('layout', array('user' => $user));

I echo $user on the layout and it displays the user information perfectly.

However, if I modify the controller by simply removing the first line that uses Auth to login and then refresh the page, it's blank.

EDIT

I am using Confide 3.1.0 for user authentication and here is the do_login() function. I have done a die & dump immediately inside of the first 'if' to confirm that Confide::logAttempt is returning true.

I have also completely hidden that entire block and simply done a 'echo Auth::user()' and it does in fact return the User model for the user I just logged in as.

public function do_login()
{
    $input = array(
        'email'    => Input::get( 'email' ), // May be the username too
        'username' => Input::get( 'email' ), // so we have to pass both
        'password' => Input::get( 'password' ),
        'remember' => Input::get( 'remember' ),
    );
    // If you wish to only allow login from confirmed users, call logAttempt
    // with the second parameter as true.
    // logAttempt will check if the 'email' perhaps is the username.
    // Get the value from the config file instead of changing the controller

    if ( Confide::logAttempt( $input, Config::get('confide::signup_confirm') ) )
    {
        // Redirect the user to the URL they were trying to access before
        // caught by the authentication filter IE Redirect::guest('user/login').
        // Otherwise fallback to '/'
        // Fix pull #145
        return Redirect::intended('/'); // change it to '/admin', '/dashboard' or something
    }
    else
    {
        $user = new User;

        // Check if there was too many login attempts
        if( Confide::isThrottled( $input ) )
        {
            $err_msg = Lang::get('confide::confide.alerts.too_many_attempts');
        }
        elseif( $user->checkUserExists( $input ) and ! $user->isConfirmed( $input ) )
        {
            $err_msg = Lang::get('confide::confide.alerts.not_confirmed');
        }
        else
        {
            $err_msg = Lang::get('confide::confide.alerts.wrong_credentials');
        }

                    return Redirect::action('UserController@login')
                        ->withInput(Input::except('password'))
            ->with( 'error', $err_msg );
    }

}

Here is the logAttempt() function

public function logAttempt( $credentials, $confirmed_only = false, $identity_columns = array() )
{
    // If identity columns is not provided, use all columns of credentials
    // except password and remember.

    if(empty($identity_columns))
    {
        $identity_columns = array_diff(
            array_keys($credentials),
            array('password','remember')
        );
    }

    // Check for throttle limit then log-in
    if(! $this->reachedThrottleLimit( $credentials ) )
    {
        $user = $this->repo->getUserByIdentity($credentials, $identity_columns);

        if(
            $user &&
            ($user->confirmed || ! $confirmed_only ) &&
            $this->app['hash']->check(
                $credentials['password'],
                $user->password
            )
        )
        {
            $remember = isset($credentials['remember']) ? $credentials['remember'] : false;

            $this->app['auth']->login( $user, $remember );
            return true;
        }
    }

    $this->throttleCount( $credentials );

    return false;
}

Upvotes: 0

Views: 1024

Answers (1)

brianrhea
brianrhea

Reputation: 3724

I'm very sorry to say that all I did was to create a clean install of Laravel and then copied my Controllers/Models/Views/Routes etc over to the new installation and it worked perfectly.

I'm certain there must be some underlying reason why it quit working and I wish I could save anyone else who comes along some time by tracking down the problem. However, after trying many different suggestions, this is what fixed it in the end.

Upvotes: 0

Related Questions