Home > Back-end >  Laravel 9 Change Password with Current Password Using API
Laravel 9 Change Password with Current Password Using API

Time:11-05

I want to change a password in Laravel 9 using api I use postman, but I get a problem that I couldn't solve I have tried more than one method and get the same error

this is route:

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

this is model : enter image description here

and this is controller :

 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);
      }
   }

And the error I get in postman is :

enter image description here

What is the problem ?

CodePudding user response:

Try it you are forgot 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);
      }
   }
  • Related