0

I am getting Not Found error whenever the validation fails whereas I should be getting the validation error. Can anyone please tell me what I am doing wrong here? The code below executes perfectly when all the inputs are available.

this is my routes/api.php

Route::group(['middleware' => 'auth:api'], function() {
    Route::post('/change-password', 'API\AuthController@changePassword');
}

this is my controller

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\User;

use Illuminate\Http\Request;


class AuthController extends Controller {

    public function changePassword(Request $request) {
        $rules = [
            'old_password' => 'required',
            'new_password' => 'required|min:6',
            'confirm_password' => 'required|same:new_password',
        ];

        $customMessages = [
            'required' => 'The :attribute field can not be blank.'
        ];

        $this->validate($request, $rules, $customMessages);

        if (! \Hash::check($request->old_password, $request->user()->password)) {
            return response()->json([
                "message" => "Please check your old password"
            ], 400 , [], JSON_NUMERIC_CHECK);
        }
        else if (\Hash::check($request->new_password, $request->user()->password)) {
            return response()->json([
                "message" => "New password is same as the old password"
            ], 400 , [], JSON_NUMERIC_CHECK);
        }
        else {
            User::where('id', $request->user()->id)->update([
                'password' => \Hash::make($request->new_password)
            ]);
            return response()->json([
                "message" => "Password updated successfully"
            ], 200 , [], JSON_NUMERIC_CHECK);
        }
    }
}
Ravgeet Dhillon
  • 532
  • 2
  • 6
  • 24

0 Answers0