postman Laravel 9使用API更改密码与当前密码

d8tt03nd  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(184)

我想使用api更改Laravel 9中的密码我使用postman,但遇到无法解决的问题我尝试了多种方法,但仍出现相同的错误
这是路由:

Route::get('changePassword', [AuthController::class, 'ChangePassword']);

这是模型:

这是控制器:

public function ChangePassword(Request $request)
   {
      $validator=Validator::make($request->all(),[
         'old_password'        =>'required',
         'new_password'         =>'required|min:8|max:30',
         'confirm_password' =>'required|same:new_password'
      ]);
      if ($validator->fails()) {
         return response()->json([
            'message'=>'validations fails',
            'errors' =>$validator->errors()
         ],422);
      }
      $user=$request->user();

      if (Hash::check($request->old_password,$user->password)) {
         $user->update([
            'password'=>Hash::make($request->password)
         ]);

         return response()->json([
            'message'=>' password successfully updated',
            'errors' =>$validator->errors()
         ],200);
      }
      else
      {
         return response()->json([
            'message'=>'old password does not match',
            'errors' =>$validator->errors()
         ],422);
      }
   }

而我在 Postman 中得到的错误是:

有什么问题吗?

k3fezbri

k3fezbri1#

请尝试忘记new_password

public function ChangePassword(Request $request)
   {
      $validator=Validator::make($request->all(),[
         'old_password'        =>'required',
         'new_password'         =>'required|min:8|max:30',
         'confirm_password' =>'required|same:new_password'
      ]);
      if ($validator->fails()) {
         return response()->json([
            'message'=>'validations fails',
            'errors' =>$validator->errors()
         ],422);
      }
      $user=$request->user();

      if (Hash::check($request->old_password,$user->password)) {
         $user->update([
            'password'=>Hash::make($request->new_password)
         ]);

         return response()->json([
            'message'=>' password successfully updated',
            'errors' =>$validator->errors()
         ],200);
      }
      else
      {
         return response()->json([
            'message'=>'old password does not match',
            'errors' =>$validator->errors()
         ],422);
      }
   }

相关问题