2016-09-19 145 views
1

所以我有一個相當奇怪的問題。我創建了一個允許用戶更改密碼的表單。Laravel 5.2 - 驗證用戶更改密碼 - 更新後密碼匹配問題

它確實更改了密碼。但它將密碼更改爲Laravel顯然無法識別的內容。因此,如果我使用「修補程序」手動更新密碼,像「測試」;我將能夠成功更改密碼。但是,一旦我將密碼更改爲某些內容(例如123456),表單將不接受密碼。

當我註銷用戶並嘗試使用新密碼登錄時;它不會讓我登錄。

很明顯,Laravel沒有認出新的密碼。

代碼是在這裏:

查看:

<div class="panel panel-default"> 
     <div class="panel-heading">Change Your Password</div> 
      {{ Form::open(array('url' => 'security/change_password')) }} 
       <div class="form-group"> 
        {!! Form::label('current_password', 'Enter Current Password:') !!} 
        {!! Form::text('current_password', null, ['class'=>'form-control']) !!} 
       </div> 

       <div class="form-group"> 
        {!! Form::label('password', 'Enter New Password:') !!} 
        {!! Form::text('password', null, ['class'=>'form-control']) !!} 
       </div> 

       <div class="form-group"> 
        {!! Form::label('password_confirmation', 'Confirm New Password:') !!} 
        {!! Form::text('password_confirmation', null, ['class'=>'form-control']) !!} 
       </div> 

       <div class="form-group"> 
        {!! Form::submit('Change Password', ['class' => 'btn btn-primary form-control']) !!} 
       </div>     
      {!! Form::close() !!} 

    </div> 

控制器:

public function updatePassword(UserSecurityFormRequest $request) 
{ 

    $user = Auth::user(); 
    $current_password = $request->input('current_password'); 
    $new_password = $request->input('password'); 
    if (Hash::check($current_password, $user->password)) { 
     $user->fill([ 
       'password' => Hash::make($request->newPassword) 
      ])->save(); 
    } 
    else{ 
     return ('Please enter the correct password'); 
    } 
} 

我也試圖設置的用戶密碼的屬性..

public function setPasswordAttribute($password) 
{ 
    return $this->attributes['password'] = bcrypt($password); 
} 
沒有工作的

ķ。 (編輯:然後我刪除了上述屬性)如果有任何事情阻止註冊表單(作爲Laravel的默認身份驗證系統的一部分)創建登錄表單識別的密碼。

我檢查過以確保表單提交的是正確的細節。當表單成功提交時,我通過從表單輸入中刪除所有數據來做到這一點。

用戶模型:

<?php 

命名空間應用; 使用Carbon \ Carbon;

使用Illuminate \ Foundation \ Auth \ User作爲Authenticatable; //使用App \ Http \ Controllers \ traits \ HasRoles;

類用戶擴展可驗證 {

/** 
* The attributes that are mass assignable. 
* 
* @var array 
*/ 
protected $fillable = [ 
    'name', 'email', 'password', 
]; 

/** 
* The attributes that should be hidden for arrays. 
* 
* @var array 
*/ 
protected $hidden = [ 
    'password', 'remember_token', 
]; 

//If register dont work or passwords arent being recognised then remove the following: 

/* public function setPasswordAttribute($password) 
{ 
    return $this->attributes['password'] = bcrypt($password); 
}*/ 

//turns dates to carbon 
protected $dates = ['created_at']; 

    //Creates Many to Many Relationship between Users table (and model) and Roles Table (and model) 
public function roles() 
{ 
    return $this->belongsToMany(Roles::class); 
} 


//Checks for a specific role 
public function hasRole($role) 
{ 
    if(is_string($role)) 
    { 
     return $this->roles->contains('name', $role); 
    } 

    return !! $role->intersect($this->roles)->count(); 
} 

//gives the user the role 
public function assignRole($role) 
{ 
    return $this->roles()->save(
     Roles::whereName($role)->firstOrFail() 
    ); 
} 

//Checks whether the user has a role with that permission 
public function hasPermission($permission) 
{ 
    return $this->hasRole($permission->roles); 
} 

public function owns($related) 
{ 
    return $this->id === $related->user_id; 
} 

}

正如你所看到的,我註釋掉的屬性設置器密碼,這樣不會影響它。但它仍然不起作用。

任何幫助,將不勝感激。

謝謝。

編輯

這是行不通的。海量感謝大家誰回答和@Steve鮑曼讓我indentify我的錯誤

工作職能: 公共職能updatePassword(UserSecurityFormRequest $要求) {

$user = Auth::user(); 
    $hashed_password = Auth::user()->password; 
    $current_password = $request->input('current_password'); 
    $new_password = $request->input('password'); 
    if (Hash::check($current_password, $hashed_password)) { 
     $user->fill([ 
           'password' => Hash::make($request->password) 

      ])->save(); 
    } 
    else{ 
     return ('Please enter the correct password'); 
    } 
} 
+0

發佈您的'用戶'型號代碼。 '$ fillable'屬性中的'password'屬性是什麼?否則,您發佈的這段代碼將無法使用。 –

+0

發佈用戶模型代碼。 「密碼」實際上是$可填寫屬性。仍然沒有運氣。 –

回答

1

找到您的問題:

public function updatePassword(UserSecurityFormRequest $request) 
{ 

    $user = Auth::user(); 

    $current_password = $request->input('current_password'); 

    $new_password = $request->input('password'); 

    if (Hash::check($current_password, $user->password)) { 

     $user->fill([ 

       // This should be $request->password, not `$request->newPassword` 

       'password' => Hash::make($request->newPassword) 

      ])->save(); 

    } else { 
     return ('Please enter the correct password'); 
    } 
} 

請求變量newPassword將是空的,這就是爲什麼你的密碼不工作。您要查找的請求字段是password

這是由於您的表單字段使用password作爲輸入名稱。

+0

哦,我的這個真的有效!非常感謝!!你是絕對的明星!我從文檔中得到了「newPassword」,認爲它是一個Laravel函數。 –

+0

完全沒問題!很高興你解決了它:)。 –

+0

當然不是沒有你的幫助。我會更新我的帖子,以防萬一有人犯了同樣的錯誤。儘管我懷疑任何人都會是那種心不在焉的人! 再次感謝您!^_ ^ –

0

你讓雙重密碼hasing,第一在哈希::使($請求 - > NEWPASSWORD)在二傳手bcrypt($密碼)

刪除一個,一切都應該沒問題。

+0

嗯,我應該說但我刪除了'公共函數setPasswordAttribute($ password) { return $ this-> attributes ['password'] = bcrypt($ password);我意識到它不起作用後,不幸的是,它仍然不起作用。 –

0

嘗試使用此:

bcrypt($request->newPassword); 
+0

我所做的: '如果(哈希::檢查($ current_password,$ hashed_pa​​ssword)){ \t $用戶>填寫([ '密碼'=> bcrypt($請求 - > NEWPASSWORD) ]) - >保存(); \t}' 不幸的是它沒有工作。 –