我正在阅读有关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;
}
}
答案 0 :(得分:3)
使用 Auth :: attempt()
登录后,会话似乎没有持续存在在用户类(型号)中,尝试添加
$primaryKey = ''; // add your user table primary key here
然后,您的postSignIn()
函数在if($auth) { ... }
内,在返回之前,尝试添加
Auth::loginUsingId(Auth::user()->userTablePrimaryKey);