我正在嘗試實作 (changePassword) 方法,該方法使用戶能夠更改他們的舊密碼,我為此使用了 (modal) 和 ajax :

資料來自ajax到(控制器)很好,但是:
- 當我將來自用戶(模態)的(oldPassword)與存盤在資料庫中的(散列的 currentPassword)進行比較時,總是說它是錯誤的,盡管(來自模態的 oldPassword)與存盤在資料庫中的密碼相同?
路線:
Route::group(['middleware'=>'auth:web'], function(){
Route::get('/profile', [ProfileController::class,'profile'])->name('profile');
Route::post('/change-password', [ProfileController::class,'changePassword'])->name('profile.changePassword');
});
控制器:
public function changePassword(ProfilePasswordRequest $request)
{
try{
$currentPass = Auth::user()->password;
if (Hash::check($request->oldPassword, $currentPass)) {
$user = User::find(Auth::id());
$user -> password = Hash::make($request->password);
$user -> save();
return response()->json([
'status' => true,
'msg' => 'Your password changed successfully',
]);
}else{
return response()->json([
'status' => false,
'msg' => 'Old password is wrong',
]);
}
}catch (\Exception $ex){
return $ex;
return redirect()->back()->with(['error' => 'Something error please try again later']);
}
}
總是回傳回應去(else),雖然(來自模態的oldPassword)存盤在資料庫中的密碼相同?
'status' => false,
'msg' => 'Old password is wrong',
常密碼請求:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ProfilePasswordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'oldPassword' => 'required',
'password' => 'required|confirmed|min:8',
];
}
}
腳本:
<script>
$(document).on('click', '#changePassword', function(e){
$("#changePassword").attr("disabled", true);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#oldPassword_error').text('');
$('#password_error').text('');
var password = $('#password').val();
var oldPassword = $('#oldPassword').val();
var passwordConfirmation = $('#password_confirmation').val();
console.log(oldPassword);
$.ajax({
type: 'post',
url: "{{route('profile.changePassword')}}",
data:{
oldPassword:oldPassword,
password:password,
password_confirmation:passwordConfirmation
},
cache: false,
success: function (response){
if(response.status===true){
$('#changPassForm')[0].reset();
$('#ChangePasswordMsgSucc').show();
$("#changePassword").attr("disabled", false);
}
if(response.status===false){
$('#changPassForm')[0].reset();
$('#ChangePasswordMsgError').show();
$("#changePassword").attr("disabled", false);
}
}, error: function (reject){
$("#changePassword").attr("disabled", false);
var response = $.parseJSON(reject.responseText);
$.each(response.errors, function(key, val){
$("#" key "_error").text(val[0]);
});
}
});
});
</script>
頁面:
<div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel2" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row mr-2 ml-2">
<button id="ChangePasswordMsgError" style="display: none" type="button" class="btn btn-lg btn-block btn-outline-danger mb-2"
>Your old password is wrong
</button>
</div>
<div class="row mr-2 ml-2">
<button id="ChangePasswordMsgSucc" style="display: none" type="button" class="btn btn-lg btn-block btn-outline-danger mb-2"
>Your old password is wrong
</button>
</div>
<div class="row d-flex justify-content-center " style="font: normal normal bold 24px/45px Cairo; color: #0D67CB">
<p class="text-center">Change your Password</p>
</div>
<form id="changPassForm">
@csrf
<div class="row mt-3 pl-3 pr-3 mr-3 ml-3 d-flex justify-content-center">
<input type="password" id="oldPassword" class="form-control" placeholder="Old Password">
<small id="oldPassword_error" class="form-text text-danger"></small>
</div>
<div class="row mt-3 pl-3 pr-3 mr-3 ml-3 d-flex justify-content-center">
<input id="password" type="password" class="form-control" placeholder="New Password">
<small id="password_error" class="form-text text-danger"></small>
</div>
<div class="row mt-3 pl-3 pr-3 mr-3 ml-3 d-flex justify-content-center">
<input id="password_confirmation" type="password" class="form-control" placeholder="Confirm New Password">
</div>
</form>
</div>
<div class="modal-footer pr-5 pt-5 pb-5">
<button type="button" class="btn btn-light">Reset</button>
<button id="changePassword" type="button" class="btn btn-warning">Change</button>
</div>
</div>
</div>
</div>
我試圖 console.log() 所有來自 ajax 的值,它成功了。
請提供任何幫助。
uj5u.com熱心網友回復:
我不是 100% 確定這是問題所在,但可以使用 -> 表示法訪問請求變數。還是你必須呼叫 ->get('name') 或 ->input('name')
if (Hash::check($request->get('oldPassword'), $currentPass)) {
$user = User::find(Auth::id());
$user->password = Hash::make($request->get('password'));
$user->save();
return response()->json([
'status' => true,
'msg' => 'Your password changed successfully',
]);
} else {
return response()->json([
'status' => false,
'msg' => 'Old password is wrong',
]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/419775.html
標籤:
上一篇:忽略一些資料的平均計算
