在Laravel 4中的Sentry中使用“记住我"功能 [英] Using the Remember me feature with Sentry in Laravel 4

查看:92
本文介绍了在Laravel 4中的Sentry中使用“记住我"功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个登录表单来记住"用户的登录信息,但我不知道该怎么做.

I'm trying to get a login form to 'remember' the user logging in and I just can't work out how to do it.

这是我的控制器

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

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

Can someone help point me in the right direction please?

推荐答案

您还可以使用助手方法:

You could also use the helper method:

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

这篇关于在Laravel 4中的Sentry中使用“记住我"功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆