在Laravel 4中使用与Sentry记住我的功能

时间:2013-10-01 08:21:51

标签: laravel login laravel-4 remember-me cartalyst-sentry

我正在尝试获取一个登录表单以“记住”用户登录,我无法理解如何操作。

这是我的控制器

public function getLogin()
{
    // Return the view with the data
    return View::make('users.login');
}

public function postLogin() 
{
    // Gather Sanitized Input
    $input = array(
        'email'      => Binput::get('email'),
        'password'   => Binput::get('password'),
        'rememberMe' => Binput::get('rememberMe')
        );

    // Set Validation Rules
    $rules = array (
        'email'    => 'required|min:4|max:64|email',
        'password' => 'required|min:6'
        );

    //Run input validation
    $v = Validator::make($input, $rules);

    if ($v->fails())
    {
        // Validation has failed
        return Redirect::to('users/login')->withErrors($v)->withInput();
    }
    else 
    {
        try
        {
            //Check for suspension or banned status
            $user = Sentry::getUserProvider()->findByLogin($input['email']);
            $throttle = Sentry::getThrottleProvider()->findByUserId($user->id);
            $throttle->check();

            // Set login credentials
            $credentials = array(
                'email'    => $input['email'],
                'password' => $input['password']
            );

            // Try to authenticate the user
            $user = Sentry::authenticate($credentials, $input['rememberMe']);
            Sentry::loginAndRemember($user);

        }
        catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
        {
            // Sometimes a user is found, however hashed credentials do
            // not match. Therefore a user technically doesn't exist
            // by those credentials. Check the error message returned
            // for more information.
            Session::flash('error', 'Invalid username or password.' );
            return Redirect::to('users/login')->withErrors($v)->withInput();
        }
        catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
        {
            echo 'User not activated.';
            Session::flash('error', 'You have not yet activated this account.');
            return Redirect::to('users/login')->withErrors($v)->withInput();
        }

        // The following is only required if throttle is enabled
        catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
        {
            $time = $throttle->getSuspensionTime();
            Session::flash('error', "Your account has been suspended for $time minutes.");
            return Redirect::to('users/login')->withErrors($v)->withInput();
        }
        catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
        {
            Session::flash('error', 'You have been banned.');
            return Redirect::to('users/login')->withErrors($v)->withInput();
        }

        return Redirect::to('/');
    }
}

/**
 * Logout
 */

public function getLogout() 
{
    Session::flush();
    Sentry::logout();
    return Redirect::to('/');
}

这是我的观点

@extends('layouts/master')

{{-- Web site Title --}}
@section('title')

@stop

{{-- Content --}}
@section('content')
<div class="tck-well span6 offset3">
    <h1>Login</h1>
    <form class="" action="{{ URL::to('users/login') }}" method="post">   
        {{ Form::token(); }}

        <div class="control-group {{ ($errors->has('email')) ? 'error' : '' }}" for="email">
            <label class="control-label" for="email">E-mail</label>
            <div class="controls">
                <input name="email" id="email" value="{{ Request::old('email') }}" type="text" class="input-xlarge" placeholder="E-mail">
                {{ ($errors->has('email') ? $errors->first('email') : '') }}
            </div>
        </div>

       <div class="control-group {{ $errors->has('password') ? 'error' : '' }}" for="password">
            <label class="control-label" for="password">Password</label>
            <div class="controls">
                <input name="password" value="" type="password" class="input-xlarge" placeholder="New Password">
                {{ ($errors->has('password') ?  $errors->first('password') : '') }}
            </div>
        </div>

        <div class="control-group" for"rememberme">
            <div class="controls">
                <label class="checkbox inline">
                    <input type="checkbox" name="rememberMe" value="1"> Remember Me
                </label>
            </div>
        </div>

        <div class="form-actions">
            <input class="button button-large button-secondary" type="submit" value="Log In"> 
            <a href="/users/resetpassword" class="btn btn-link">Forgot Password?</a>
        </div>
  </form>
</div>

@stop

有人可以帮我指出正确的方向吗?

5 个答案:

答案 0 :(得分:3)

您也可以使用辅助方法:

if( Input::get('rememberMe') ) {
    $user = Sentry::authenticateAndRemember($credentials)
} else {    
    $user = Sentry::authenticate($credentials, false);
}

答案 1 :(得分:1)

与Devo的相似

// Try to log the user in
Sentry::authenticate(Input::only('email', 'password'), Input::get('remember-me', 0));

// For the view page
<input type="checkbox" name="remember-me" id="remember-me" value="1" /> Remember me;

答案 2 :(得分:0)

而不是,

$user = Sentry::authenticate($credentials, $input['rememberMe']);

使用,

if(!empty($input['rememberMe'])) {
   $user = Sentry::authenticate($credentials, true);
} else {
   $user = Sentry::authenticate($credentials, false);
}

确保您在$input['rememberMe']中获得了一些价值。

答案 3 :(得分:0)

从GitHub看来,似乎有时需要在php.ini(或.htaccess)中设置gc_maxlifetime ..

session.gc_maxlifetime = 2592000

答案 4 :(得分:0)

在app / config / session.php中添加以下行:

'lifetime' => 999999,
'expire_on_close' => false,