我正在开发一个 Laravel 项目,并且创建了一个密码更改 API。这是我正在使用的路线:
Route::post('/change-password', function (Request $request) {
// Validate the request data
$request->validate([
'current_password' => 'required|string',
'new_password' => 'required|string|confirmed|min:8',
]);
// Check if the current password matches the user's current password
if (!Hash::check($request->current_password, $request->user()->password)) {
return response()->json([
'message' => 'The provided current password does not match your current password.',
], 422);
}
// Update the user's password
$request->user()->password = bcrypt($request->new_password);
$request->user()->save();
return response()->json([
'message' => 'Password has been changed successfully.',
], 200);
})->middleware('auth:sanctum')->name('password.change');
当我测试这个时,我得到状态代码 200,但是密码没有更改。 [我不是在寻找重置密码,我是在寻找经过身份验证的用户的更改密码。]
我尝试过:user()->updated(),AI聊天机器人。
像这样更新您的验证规则
$request->validate([
'current_password' => 'required|current_password',
'new_password' => 'required|string|confirmed|min:8',
]);