簡體   English   中英

Laravel Auth :: Attempt僅在第一次驗證

[英]Laravel Auth::Attempt authenticates only first time

我和Laravel有一個奇怪的問題。

正常創建用戶后,用戶可以第一次登錄,但在此之后, Auth::attempt總是失敗。 我只能通過代碼更新用戶密碼再次登錄,當我檢查數據庫時,所有密碼都被正確哈希。

我似乎無法理解為什么會這樣。 這是我的代碼

注冊方法

$user = new User;
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Input::get('password');
$user->save();
$general = Role::find(3);
$user->roles()->attach($general->id);

登錄方式

$username = Input::get('username');
$pwd = Input::get('password');
if (!($pwd && $username)) {
    return Redirect::intended('user/login')->withMessage('Emtpy username/password')->withInput();
}
if (Auth::attempt(array('username' => $username, 'password' => $pwd))){
    return Redirect::intended('user/dashboard');
}
elseif (Auth::attempt(array('email' => $username, 'password' => $pwd))) {
    return Redirect::intended('user/dashboard');
}
else {
    return Redirect::intended('user/login')->withMessage('Incorrect username/password')->withInput();
}

用戶模型

public static function boot()
{
    parent::boot();

    static::saving(function($data) {
        $data->password = Hash::make($data->password);
        unset($data->password_confirmation);
        return true;
    });
}

問題是你使用了錯誤的事件監聽器

您錯誤地掛鈎了save事件, 每當用戶模型發生更改時(即creatingupdating )都會調用該事件。

這意味着每次用戶登錄並執行某些操作(更新登錄計數,更改其名字等) - 它會導致模型重新散列已經散列的密碼,從而導致其被破壞。

有兩種方法可以解決這個問題。

選項一是將事件更改為僅適用於creating事件。 但這意味着稍后當您需要更新用戶密碼時,它將無法正確重新散列,因此選項2更好。

選項2是不使用任何事件, 而只使用賦值函數功能 -這是專為這個確切的情況。

class User extends Eloquent {

    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

}

這樣,無論何時/何時更改用戶密碼,都將進行哈希處理,但僅在實際需要更改時才進行哈希處理。

我能夠通過覆蓋默認的eloquent save方法來解決我的問題,並在創建用戶時手動創建哈希。 這是代碼:

public function save(array $options = array())
{
    if (isset($this->password_confirmation))
    {
        unset($this->password_confirmation);
    }
    if (isset($this->password))
    {
        if (Hash::needsRehash($this->password))
        {
            $this->password = Hash::make($this->password);
        }
    }
    parent::save();
}

但是,我仍然無法一遍又一遍地發現為什么這種奇怪的經歷正在發生。 我仍然有代碼重現錯誤(即使從2014年7月22日起干凈安裝Laravel 4.2),並且可以提供給有興趣調查此問題的任何人。

我使用的包是Zicaco Entrust和Laravel Frozennode。

整個方法是復雜的方法。 做起來很簡單。

將您的注冊方法更改為:

$user = new User;
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
$general = Role::find(3);
$user->roles()->attach($general->id);

扔掉你的“靴子”方法。

另外(不太相關,但我喜歡這樣做)你可以將登錄程序改為:

$username = Input::get('username');
$pwd = Input::get('password');
if (!($pwd && $username)) {
    return Redirect::intended('user/login')->withMessage('Emtpy username/password')->withInput();
}
if (Auth::attempt(array('username' => $username, 'password' => $pwd)) 
    || Auth::attempt(array('email' => $username, 'password' => $pwd))){
        return Redirect::intended('user/dashboard');

    }
    else {
        return Redirect::intended('user/login')->withMessage('Incorrect username/password')->withInput();
    } 

在我看來你的問題在這里:

if (Auth::attempt(array('username' => $username, 'password' => $pwd))){
***return Redirect::intended('user/dashboard');***
}
elseif (Auth::attempt(array('email' => $username, 'password' => $pwd))) {
    return Redirect::intended('user/dashboard');

如果您的第一次Auth嘗試使用用戶名失敗,它將在第二次Auth嘗試阻止之前返回重定向方法。 這可能不是您所面臨的整體問題,但它似乎仍然是一段錯誤的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM