2016-10-22 160 views
0

要更改密碼,我輸入當前password.For正確的密碼也是錯誤的味精會顯示如何檢查輸入的密碼是否爲當前密碼?

公共職能passwordupdate(請求$要求){

$user=user::find(Auth::user()->id); 

    $validator=Validator::make($request->all(), [ 
     //'password' => 'required|confirmed|min:6|max:14' 
     'cpassword'=> 'required|confirmed|min:6|max:14'      
    ]); 
    if($validator->fails()){ 
     return redirect()->back()->withErrors(['cpassword'=>'Please check the password you given']); 
    }else{ 
     $user->cpassword=bcrypt($request->cpassword); 
     $user->save(); 
     return redirect()->back(); 
    } 

} 
+0

嘿,請,如果有任何標記的答案是正確的 –

回答

0

你可以使用Hash::check()方法。例如,如果你想比較輸入的密碼當前用戶的一個:

if (Hash::check($request->password, auth()->user()->password) { 
    // Entered password is correct. 
1
  1. 首先,你需要use Illuminate\Support\Facades\Hash;

  2. 檢查舊用戶輸入的密碼,您可以使用Hash::check()。該功能需要2個參數。 第一個是純字符串,第二個是哈希密碼(舊密碼)。

您可以檢索由Auth::user()->password舊密碼,所以你如果條件是

if(Hash::check($request['oldpass'], $user->password)){ 
    //insert the new password 
    Auth::user->update([ 
    'password'=>bcrypt($data['newpass']) 
    ]); 
    Auth::user->save(); 
} 

P.S Hash::check()返回TRUE或FALSE