首页 文章

Laravel Auth ::尝试工作然后Auth :: check失败

提问于
浏览
0

我是Laravel的新手,并且一直关注有关身份验证的教程,但有些东西不起作用 . Auth ::尝试似乎正常 . 当它失败时,我被重定向回登录有错误,当它成功时,我被重定向到我的博客索引 . 可悲的是,在那之后我无法访问任何受保护的页面!我只是被重定向到我的登录页面 . 它让我疯了!

我在博客索引视图中转储了一些变量,这些变量在Auth :: attempt成功查看正在发生的事情时呈现:

@section('content')
<h1>This is a blog!!</h1>

<?php
    echo var_dump(Session::all());
    echo var_dump(Auth::check());
    foreach($posts as $post) { ?>
        <div class="post">
        <?php
            echo "<h3><a href=\"/blog/$post->slug\">{$post->title}</a></h3>";
            echo "{$post->content}";
        ?>
        </div>
    <?php } ?>
@stop

当我var_dump Session :: all()我得到这个数组:

array(4) { ["_token"]=> string(40) "0hqRso6my0VpvQVtrXlLDeKioglu4xNXl5pbpNGM" ["flash"]=> array(2) { ["old"]=> array(0) { } ["new"]=> array(0) { } } ["url"]=> array(1) { ["intended"]=> string(49) "http://temporal-storm-743.appspot.com/blog/create" } ["login_82e5d2c56bdd0811318f0cf078b78bfc"]=> NULL }

当我var转储Auth :: check我得到假:bool(假)

我将在login_ur4iafo9之后假设NULL为什么意味着会话没有成功登录用户?

我没有更改我在filters.php中默认应用于受保护页面的auth过滤器 .

我检查了我的session.php文件:

'driver' => 'memcached',
'lifetime' => 120,
'expire_on_close' => false,

和我的auth.php

'driver' => 'eloquent',
'model' => 'User',

和我的模特......

class User extends Eloquent implements UserInterface, RemindableInterface {

  use UserTrait, RemindableTrait;

  public $timestamps = false;

  protected $table   = 'WebUser';
  protected $primaryKey   = 'WebUserId';
  protected $hidden   = array('password', 'remember_token');

}

当然我的登录控制器方法:

public function doLogin()
{
    $rules = array(
        'email'=>'required|email',
        'password'=>'required|min:8'
    );
    $validator = Validator::make(Input::all(), $rules);
    if( $validator->fails() )
        return Redirect::back()->withInput(Input::except('password'))->withErrors($validator);

    $userdata = array(
        'email'=>Input::get('email'),
        'password'=>Input::get('password'),
    );

    if ( !Auth::attempt($userdata, true) )
        return Redirect::back()->withErrors(['email'=>'Login failed.']);

   return Redirect::to('blog');
}

我不知道你们发生了什么事 .

EDIT: 我知道你现在发生了什么事 .

度假后我回到了这个问题,并开始在认证文件和论坛中探索并尝试了一些不同的auth方法 . 我认证工作正常!

我仍然不是100%确定原因,但Eloquent驱动程序似乎不适用于Google App Engine . 从Eloquent更改为数据库身份验证后,更新我的数据库以便将所有主键设置为id并添加 remember_token VARCHAR(100) NULL 作为字段似乎正在进行身份验证 .

也许拥有更多Laravel经验并且更了解不同提供商和身份验证工作方式的人可以帮助我理解为什么只有数据库身份验证才能在Google应用引擎上运行?

2 回答

  • 1

    我有同样的问题,并寻找了几个小时的解决方案 .

    然后我改变了:

    if(Auth::check()) return Redirect::to("/admin");
    

    至:

    if( ! Auth::guest()) return Redirect::to("/admin");
    

    这对我有用.. :)

  • 2

    我认为Laravel有一个bug . 如果您使用Auth ::尝试验证您的凭证然后返回true并“销毁会话” . 所以我们重定向我们的url并使用Auth :: check()使其返回false . 因为会话被破坏而你丢失了要检查的数据 .

相关问题