【发布时间】: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