首页 文章

重定向到页面后,Laravel Auth :: get()失败

提问于
浏览
0

我正在阅读有关Laravel身份验证的教程,以了解更多信息 . 我已经达到了一个我无法继续前进的地步,因为我很困惑 .

恢复问题:我做了一个Auth ::尝试,它可以工作,但是一旦我重定向到一个页面,Auth :: get()就不再起作用了 .

这是我的控制器:

public function postSignIn()
{
    $validator = Validator::make(Input::all(),
        array(
            'usrUsername'   => 'required'
            ,'usrPassword'  => 'required'
        ));

    if($validator->fails()){
        return Redirect::route('account-sign-in-get')
            ->withErrors($validator)
            ->withInput();
    }else{

        $auth = Auth::attempt(array(
            'usrUsername' => Input::get('usrUsername')
            ,'password' => Input::get('usrPassword')
            ,'usrIsActive' => 1
        ));

        if($auth){
            return Redirect::intended('/')
                ->with('global', 'Signed in. Have a great adventure!');
        }else{
            return Redirect::route('account-sign-in-get')
                ->with('global', 'Are you sure that is your correct username/password combination?');
        }
    }

}

这是我在navigation.blade.php上的html方面:

@if(Auth::check())
        <li><a href="{{ URL::route('account-logout') }}">Sign Out</a></li>
    @else
        <li><a href="{{ URL::route('account-create-get') }}">Create account</a></li>
        <li class="nav-divider"></li>
        <li><a href="{{ URL::route('account-sign-in-get') }}"><i class="glyphicon glyphicon-off"></i> Sign In</a></li>
    @endif

当我在登录后进入我的重定向视图时,我可以看到全局消息,但Auth :: get()始终为false . 我错过了什么?

编辑:我在if($ auth)之后使用Auth :: check()放置了一个if条件,并且它进入了TRUE值 . 所以是的,在重定向上它似乎丢失了所有的Auth信息?

Edit2:我的用户如有必要:

class User extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

protected $fillable = array('usrEmail', 'usrUsername', 'usrPassword', 'usrCode', 'usrIsActive');

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('usrPassword', 'usrCode');

// Tell eloquent the pasword field is "usrPassword"
public function getAuthPassword() {
    return $this->usrPassword;
}

}

1 回答

  • 3

    您使用 Auth::attempt() 登录后似乎没有持久会话

    user class (模型)中,尝试添加

    $primaryKey = ''; // add your user table primary key here
    

    然后,你的 postSignIn() 函数,在 if($auth) { ... } 之内,在返回之前,尝试添加

    Auth::loginUsingId(Auth::user()->userTablePrimaryKey);
    

相关问题