【问题标题】:Laravel 4 authentication with email and IDLaravel 4 使用电子邮件和 ID 进行身份验证
【发布时间】:2013-07-16 09:28:53
【问题描述】:

基本上我希望允许用户使用他们的电子邮件地址和密码登录,但使用自动递增的 ID 作为用户表的主键。

Some tutorials 似乎表明这是非常直接的。但这似乎对我不起作用。首先,我必须更改 User 模型以使用“电子邮件”作为身份验证标识符。这可行,但会话不保存(见下文)。

用户表迁移文件;

Schema::create("users", function ($table) {
  $table->increments("id");
  $table->string("email", 255)->unique();
  $table->string("password", 60);
  $table->timestamps();
});

用户模型文件;

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {
  protected $table = 'users';
  protected $hidden = array('password');

  public function getAuthIdentifier() {
    return $this->email;
  }

  public function getAuthPassword() {
    return $this->password;
  }

  public function getReminderEmail() {
    return $this->email;
  }
}

登录;

Auth::attempt(array("email" => Input::get("email"), "password" => Input::get("password")), true);

... 有效(如返回 true)。但是,会话没有正确保存。任何后续请求都不会经过身份验证。我尝试了不同的会话驱动程序,但没有成功。

如果我添加;

protected $primaryKey = "email";

对于用户模型,会话有效。 但是这是不对的,因为“email”不是该表的主键,“id”是(我想保留“id”作为主键)。


另外:official documentation 似乎表明您不必为使用“电子邮件”作为标识列而进行任何特殊操作,就像“电子邮件”一样使用here,但随后使用数字ID here。我在这里遗漏了什么明显的东西吗?

【问题讨论】:

    标签: php authentication laravel


    【解决方案1】:

    我昨天花了所有的时间来解决这个问题,现在我基本上将 User 模型恢复到了最初的样子;

    public function getAuthIdentifier() {
      return $this->getKey();
    }
    

    ...它正在工作(我可以使用电子邮件/密码登录,我的会话让我保持登录状态)。

    我不知道为什么昨天这不起作用。与创建会话哈希的方式有关吗?我不知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 2018-02-12
      • 2019-06-11
      • 2018-09-03
      • 2018-06-11
      • 2017-10-26
      • 1970-01-01
      相关资源
      最近更新 更多