1
votes

In my project I have one page to handle login form, register form and password reset form.

For the first two, everything work well. For the last, I find a way to send my own email instead of laravel's default email but now I want to stop redirect on success.

For the story, I launch a sweetalert notification when I click on submit, call reset controller with Ajax and wait for response.

But instead, page is reload on success. I don't find a way to prevent redirect and just send json to my Ajax.

My login.js :

$(".m_login_forget_password_submit").click(function(l){

    var t=$(this),r=$(this).closest("form");
    t.addClass("m-loader m-loader--right m-loader--light");

    swal({
        title:"Mot de passe",
        text:"Génération du lien.",
        type:"info"
    });
        var $this = $(this);
        $.ajax({
            type: 'POST',
            url: 'password/request',
            data: $this.serializeArray(),
            success:function(data){
                $("#m_login_signup_submit").toggleClass("m-loader m-loader--right m-loader--light");
                swal({
                    title:"Succès",
                    text:"Votre demande de compte a été transmise à nos équipe.",
                    type:"success"
                });

            },
            error: function(data){
                $("#m_login_signup_submit").toggleClass("m-loader m-loader--right m-loader--light");
                if( data.status === 422 ) {
                    var error = "Les erreurs suivantes ont été trouvées :";
                    var errors = data.responseJSON;
                    $.each( errors, function( key, value ) {
                        error += '<br>&#9656 ' + value[0];
                    });
                    swal("Erreur",error,"error")
                }
            }
        });

My custom ResetPasswordController :

class ResetPasswordController extends Controller

{

use ResetsPasswords;

/**
 * Where to redirect users after resetting their password.
 *
 * @var string
 */
protected $redirectTo = '/home';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}

/**
 * Get the response for a successful password reset.
 *
 * @param  string  $response
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
 */
protected function sendResetResponse($response)
{
    return 'ok';
}


    }

Someone know how to prevent redirect on send email reset password success and send json instead ?

Thank for your help.

1
What version of Laravel are you using? - Rwd
I use version 5.6 of laravel - Furya

1 Answers

1
votes

When resetting a password, the reset function will be called in your ResetPasswordController, so you'll have to rename your sendResetResponse function to reset (since you want to override the trait function) and point the default trait function to an unused function. Plus, if you want a JSON response, you'll need to specify that with the return.

Example:

class ResetPasswordController extends Controller
{
    use ResetsPasswords {
        reset as unused;
    }

    ...

    //Override the trait function
    public function reset(Request $request) {
        //Call default validation and handle password change

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

        //Return the response
        if ($response == Password::PASSWORD_RESET) {
            return response()->json(array(
                'result' => 'ok',
            ), 200);
        } else {
            return response()->json(array(
                'result' => 'fail',
            ), 500);
        }
    }
}

Edit:

Sorry about that, thought you were talking about the reset password redirection. To handle the redirect after sending the email, you will take a similar approach and modify your ForgotPasswordController and override the sendResetLinkEmail function instead:

class ForgotPasswordController extends Controller
{
    use SendsPasswordResetEmails {
        sendResetLinkEmail as unused;
    }

    ...

    public function sendResetLinkEmail(Request $request)
    {
        $this->validateEmail($request);

        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );

        //Return the response
        if ($response == Password::RESET_LINK_SENT) {
            return response()->json(array(
                'result' => 'ok',
            ), 200);
        } else {
            return response()->json(array(
                'result' => 'fail',
            ), 500);
    }
    }
}