Created
November 28, 2017 09:23
-
-
Save niraj-shah/8e56b6d96d48ca4840a7dfcbcff2811d to your computer and use it in GitHub Desktop.
Using the ResetsPasswords trait in Laravel 5.2 to trigger a password reset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Http\Requests; | |
use Illuminate\Http\Request as LaravelRequest; | |
use Illuminate\Foundation\Auth\ResetsPasswords; | |
use Redirect; | |
use Request; | |
use Response; | |
use App\User; | |
class UsersController extends Controller | |
{ | |
use ResetsPasswords; | |
// ... | |
public function password_reset($id) | |
{ | |
// get the user's record | |
$user = User::find($id); | |
if ($user === null) { | |
// ajax response if user not found | |
return Response::json([ 'result' => 'error', 'message' => 'User not found.' ]); | |
} | |
try { | |
// create new Illuminate\Http\Request | |
$request = new LaravelRequest(); | |
// merge in the user's email address | |
$request->merge([ 'email' => $user->email ]); | |
// trigger password reset email | |
$this->sendResetLinkEmail($request); | |
// return json response | |
return Response::json([ 'result' => 'success', 'message' => 'Reset email has been resent to ' . $user->email ]); | |
} catch (\Exception $e) { | |
// error | |
return Response::json([ 'result' => 'error', 'message' => $e->getMessage() ]); | |
} | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment