1

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

一些教程似乎表明这是非常直接的。但这似乎对我不起作用。首先,我必须更改 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";

对于用户模型,会话工作。但这不可能是正确的,因为“电子邮件”不是该表的主键,“id”是(我想保留“id”作为主键)。


另外:官方文档似乎表明您不必做任何特别的事情来使用“电子邮件”作为标识列,因为这里使用了“电子邮件” ,但是这里使用数字 ID 。我在这里遗漏了一些明显的东西吗?

4

1 回答 1

1

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

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

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

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

于 2013-07-16T10:15:53.270 回答