Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Send Password Reset Link using Ajax

I have this code:

jQuery.ajax({
    type:"POST",
    url:"/password/email/",
    data:{
        _token: jQuery("#forgotPasswordContainer input[name='_token']").val(),
        email: email
    },
    dataType:'json',
    beforeSend:function(){

    },
    success:function(data){

    },
    complete:function(){

    }
});

it seems that it is doing nothing.

When I checked firebug, i am getting an html page containing the html of /password/email page.

I am guessing I need to modify how sending password reset link works.

Can someone help me on this matter.

Your help will be greatly appreciated!

Thanks!

like image 402
PinoyStackOverflower Avatar asked Mar 17 '23 03:03

PinoyStackOverflower


2 Answers

Ok, I managed to solved this one by putting this on my PasswordController.php

public function getEmail(Request $request)
{
    $this->validate($request, ['email' => 'required|email']);

    $response = $this->passwords->sendResetLink($request->only('email'), function($m)
    {
        $m->subject($this->getEmailSubject());
    });

    switch ($response)
    {
        case PasswordBroker::RESET_LINK_SENT:
            return[
                'error'=>'false',
                'msg'=>'A password link has been sent to your email address'
            ];

        case PasswordBroker::INVALID_USER:
            return[
                'error'=>'true',
                'msg'=>"We can't find a user with that email address"
            ];
    }
}

I am not sure if this is efficient but this works for me. Hope this helps someone.

Thanks!

like image 99
PinoyStackOverflower Avatar answered Mar 18 '23 17:03

PinoyStackOverflower


Laravel 5.2

If you want to customized or changed the POST URL that you send via AJAX here is the complete answer:

ajax.js:

jQuery.ajax({
    type:"POST",
    url:"/user/password/reset",
    data:{
        _token: jQuery("#forgotPasswordContainer input[name='_token']").val(),
        email: email
    },
    dataType:'json',
    beforeSend:function(){

    },
    success:function(data){

    },
    complete:function(){

    }
});

routes.php:

Route::post('user/password/reset', [
      'uses'         => 'Controller_name@index'
]);

App/Http/Controllers/Controller_name.php:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Illuminate\Contracts\Auth\PasswordBroker;

class Controller_name extends Controller
{
    public function index(Request $request, PasswordBroker $passwords)
    {
        if( $request->ajax() )
        {
            $this->validate($request, ['email' => 'required|email']);

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

            switch ($response)
            {
                case PasswordBroker::RESET_LINK_SENT:
                   return[
                       'error'=>'false',
                       'msg'=>'A password link has been sent to your email address'
                   ];

                case PasswordBroker::INVALID_USER:
                   return[
                       'error'=>'true',
                       'msg'=>"We can't find a user with that email address"
                   ];
            }
        }
        return false;
    }
}

resources/views/

Create a new directory auth > emails > password.blade.php for email template:

Click here to reset your password .<br />
<a target="_blank" href="{{ url('password/reset', $token).'?email='.urlencode($user->getEmailForPasswordReset()) }}">Click to Reset Password</a>
like image 29
Rashedul Islam Sagor Avatar answered Mar 18 '23 17:03

Rashedul Islam Sagor