简体   繁体   中英

Laravel password reset with AJAX (AngularJS)

I'm building app with Laravel 5.7 and angularjs. I'd like to make authorization via ajax. Login and Registration works fine, but reset passwords works incorrectly. The problem is rhere is no error or success callback message. I noticed that my request goes to the right route but then it redirects me to my home page. So in a response I always get my index page.

By default, laravel throws an error "We can't find a user with that e-mail address." But if you'll use ajax it won't throw anything and I don't know where is the problem, because login and registration throws errors successfully.

//HTML template
<form ng-submit="auth('{{ route('password.email') }}')">
    @csrf
    <div>
        <div>Enter your e-mail</div>
        <div>We will send you an email with a link to create a new password.
        </div>
    <div ng-repeat="error in authErrors.errors">@{{error[0]}}</div>
    </div>
    <div>
        <label for="email">{{ __('E-Mail Address') }}</label>
        <input name="email" ng-model="authInfo.email" type="email" required>
    </div>
    <button type="submit" name="submit">{{ __('Send Password Reset Link') }}</button>
</form>
//JS
    $scope.authInfo = {
        name: null,
        email: null,
        password: null,
        passwordConfirmation: null,
        remember: false
    }
    $scope.authErrors = {};

    $scope.auth = function(route){
        $http({
            method: 'POST',
            url: route,
            data: $scope.authInfo
        }).then(function(response) {
            console.log($scope.authInfo);
            console.log(response);
        }, function(response) {
            console.log($scope.authInfo);
            $scope.authErrors = response.data;
            console.log(response.data)
        })
    }

Auth controllers are all default.
I've tested ajax on the clear project also and got the same result. If I won't send an email as data it'll throw an error, that there is no email. But if I'll send an email I'll get just redirect.
I'd like to get the same result as registration and login functions.

Any suggestions how to fix this, please?

It is behaving as expectation, as this function will return html in both success and fail scenario. What you have to do is override the functions like below -

public function sendResetLinkEmail(Request $request)
{

    if($request->ajax()) {

        $this->validateEmail($request);

        // We will send the password reset link to this user. Once we have attempted
        // to send the link, we will examine the response then see the message we
        // need to show to the user. Finally, we'll send out a proper response.
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );

        return $response == Password::RESET_LINK_SENT
            ? [
                'msg' => 'Successful'
            ]
            : [
                'msg' => 'Reset email sending failed.'
            ];
    }

    return [
        'msg' => 'Un-expected method calls'
    ];
}

Put the function in ForgotPasswordController

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