Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Created November 28, 2017 09:23
Show Gist options
  • Save niraj-shah/8e56b6d96d48ca4840a7dfcbcff2811d to your computer and use it in GitHub Desktop.
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
<?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