我正在学习有关Laravel身份验证的教程以了解更多信息。我已经到了不能再进一步的地步,因为我很困惑。
问题的恢复:我做了一个Auth::try,它可以工作,但是一旦我重定向到一个页面,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() 总是假的。我错过了什么?
编辑:我在if($auth)之后放置了一个带有Auth::check()的if条件,它进入了TRUE值。所以是的,在重定向上似乎丢失了所有身份验证信息?
编辑2:如有必要,我的用户:
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;
}
}
使用 Auth::try() 登录后,会话似乎没有保留
在您的用户类(模型)中,尝试添加
$primaryKey = ''; // add your user table primary key here
然后,你的postSignIn()
函数,在if($auth) { ... }
里面,在返回之前,尝试添加
Auth::loginUsingId(Auth::user()->userTablePrimaryKey);