【问题标题】:Laravel Auth::get() fails after redirecting to pageLaravel Auth::get() 重定向到页面后失败
【发布时间】:2015-01-26 13:57:08
【问题描述】:

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

问题的继续:我做了一个 Auth::attempt,它可以工作,但是一旦我重定向到一个页面,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 值。所以是的,在重定向时,它似乎丢失了所有的身份验证信息?

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;
}

}

【问题讨论】:

  • 我想你的意思是Auth::check(),而不是Auth::get()
  • 是的,这是正确的 Kestutis。我将编辑文本,这样就可以了。谢谢你。

标签: php session authentication laravel


【解决方案1】:

您使用 Auth::attempt()

登录后会话似乎没有持续存在

在您的用户类(模型)中,尝试添加

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

然后,你的postSignIn()函数,在if($auth) { ... }里面,在return之前,尝试添加

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

【讨论】:

  • 我会尽快尝试 Raffy Cortez。非常感谢您的洞察力。
  • 为什么教程中没有提到这个? (我使用composer脚手架上的默认代码)
猜你喜欢
  • 2018-08-22
  • 1970-01-01
  • 2015-09-25
  • 2017-03-24
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 2016-10-24
相关资源
最近更新 更多