简体   繁体   中英

How to rewrite a method for using with AJAX?

I have a trait which handle password restoring logic:

public function reset(Request $request)
{
    $this->validate($request, $this->rules(), $this->validationErrorMessages());

    $response = $this->broker()->reset(
        $this->credentials($request), function ($user, $password) {
            $this->resetPassword($user, $password);
        }
    );

    return $response == Password::PASSWORD_RESET
                ? $this->sendResetResponse($response)
                : $this->sendResetFailedResponse($request, $response);
}

protected function rules()
{
    return [
        'token' => 'required',
        'email' => 'required|email',
        'password' => 'required|confirmed|min:6',
    ];
}

protected function sendResetFailedResponse(Request $request, $response)
{
    return redirect()->back()
                ->withInput($request->only('email'))
                ->withErrors(['email' => trans($response)]);
}

I want to use it with AJAX calls. How should I rewrite sendResetFailedResponse() ?
When I use this logic without AJAX and if validation fails on rules() I simply get an error response with 422 status code. But if validation fails while on checking token validity ( reset() ) - there are no errors with status code in return.
My AJAX is like

axios.post('/password/reset', {
                    //data to send
                })
                .then((response) => {
                    ...
                })
                .catch((error) => {
                    //I can catch errors which are returning from rules() fail
                    //I want to catch non-valid token error here too
                }); 

I tried to override

protected function sendResetFailedResponse(Request $request, $response)
{
    return response(['email' => trans($response)]);
}

but this code returns token error after AJAX .catch()

I just do this in the reset method and it works pretty good.

public function reset(Request $request)
{
    $this->validate($request, $this->rules(), $this->validationErrorMessages());
    // Here we will attempt to reset the user's password. If it is successful we
    // will update the password on an actual user model and persist it to the
    // database. Otherwise we will parse the error and return the response.
    $response = $this->broker()->reset(
        $this->credentials($request), function ($user, $password) {
            $this->resetPassword($user, $password);
        }
    );
    if ($request->ajax()){
        if ($response == Password::PASSWORD_RESET) {
            return response()->json(['message' => 'Success'],200);
        } else {
            return response()->json(['error' => 'Please try again'], 422);
        }
    }
    // If the password was successfully reset, we will redirect the user back to
    // the application's home authenticated view. If there is an error we can
    // redirect them back to where they came from with their error message.
    return $response == Password::PASSWORD_RESET
    ? $this->sendResetResponse($response)
    : $this->sendResetFailedResponse($request, $response);
}

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM