Laravel: resetting password without getting redirect response

I am building an angular application and want to implement password reset. However, default laravel config doesn’t appear to allow one to do this using purely XMLHttpRequest ($http.post) requests and responds with a 302 redirect.

I managed to get postLogin and postRegister to work without issuing redirects by implementing said methods in authController class and returning a json response, doing this overrides the default laravel implementation of said methods. No such luck with postEmail and it appears the method is not hit at all, I just get a 302 response back immediately.

Ideally, other than to check their E-mail, I don’t want the user to leave the single page angular application at all. So 1. User posts E-mail to postEmail -> Email with reset link or better ‘reset code’ is sent to E-mail address -> User then inputs the reset token code into the already open web app or if it can’t be done, browse to reset password page opened in new tab.

I tried implementing postEmail method as such:

public function postEmail(Request $request)
            {

                $this->validate($request, ['email' => 'required|email']);

                $response = Password::sendResetLink($request->only('email'), function (Message $message) {
                    $message->subject($this->getEmailSubject());
                });

                switch ($response) {
                    case Password::RESET_LINK_SENT:


                         return response()->json(['msg' => 'A reset link has been sent to your E-mail'], 200);

                    case Password::INVALID_USER:


                         return response()->json(['msg' => 'This E-mail cannot be found in our system'], 200);
                }
            }

Also, where is template for the E-mail with the reset link that laravel sends out ?

Answer

You can create a PasswordController within the AppHttpControllersAuth namespace to extend the password reset methods.

<?php 

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use IlluminateContractsAuthGuard;
use IlluminateContractsAuthPasswordBroker;
use IlluminateFoundationAuthResetsPasswords;

class PasswordController extends Controller 
{
    use ResetsPasswords;

    public function postEmail(Request $request)
    {

    }

}

To overwrite the email templates you can create a reminder.blade.php in the app/views/emails/auth directory, or change the location of the template file in the app/config/auth.php config.

Leave a Reply

Your email address will not be published. Required fields are marked *