2013-07-16 120 views
1

基本上我想讓用戶使用他們的電子郵件地址和密碼登錄,但使用自動遞增的ID作爲Users表主鍵。Laravel 4使用電子郵件和身份驗證進行身份驗證

Some tutorials似乎表明這是非常直接的。但是,這似乎並沒有爲我工作。首先,我不得不改變用戶模型以使用「email」作爲身份驗證標識符。這有效,但會話不保存(見下文)。

用戶表遷移文件;

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

...作品(如在返回中)。然而,會話不能正確保存。任何後續請求都未經過身份驗證。我嘗試過不同的會話驅動程序,但沒有成功。

如果我加;

protected $primaryKey = "email"; 

給User模型,會話正常工作。 但是不能正確,因爲「email」不是該表的主鍵,「id」是(並且我想保留「id」作爲主鍵)。


另外:official documentation似乎表明你沒有做任何事情特別使用「電子郵件」爲標識列,如「電子郵件」時here,但隨後的數字ID使用here。我在這裏錯過了很明顯的東西嗎

回答

1

我花了整個昨天試圖解決這個問題,現在我已經基本上恢復了用戶模型回到它是如何開始的;

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

...它工作(我可以用電子郵件/密碼登錄,而我的會話讓我登錄)。

我不知道爲什麼昨天不工作。與會話哈希創建的方式有關?我不知道。