Laravel 4:用户未保持登录状态

问题描述 投票:0回答:1

将 Laravel 从 4.0 升级到 4.1 后,我的用户身份验证失败了。

我按照文档提供的升级指南进行操作。

现在,当我登录时,它似乎只持续一个请求。

例如,在我的 HomeController 索引方法中,我像这样强制登录:

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

我在布局上 echo $user ,它完美地显示了用户信息。

但是,如果我通过简单地删除使用 Auth 登录的第一行然后刷新页面来修改控制器,则它是空白的。

编辑

我使用 Confide 3.1.0 进行用户身份验证,这是 do_login() 函数。我已在第一个“if”内立即执行了 die & dump,以确认 Confide::logAttempt 返回 true。

我还完全隐藏了整个块,只是做了一个“echo Auth::user()”,它实际上返回了我刚刚登录的用户的用户模型。

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

}

这是 logAttempt() 函数

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;
}
php laravel laravel-4
1个回答
0
投票

我很遗憾地说,我所做的就是创建一个全新的 Laravel 安装,然后将我的控制器/模型/视图/路由等复制到新安装中,并且它运行得很好。

我确信它停止工作一定有一些根本原因,我希望我可以通过追踪问题来拯救其他人。然而,在尝试了许多不同的建议之后,这就是最终解决它的方法。

© www.soinside.com 2019 - 2024. All rights reserved.