|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Auth; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use Illuminate\Foundation\Auth\SendsPasswordResetEmails; |
| 7 | +use Illuminate\Http\JsonResponse; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Illuminate\Support\Facades\Password; |
| 10 | + |
| 11 | +class ForgotPasswordController extends Controller |
| 12 | +{ |
| 13 | + /* |
| 14 | + |-------------------------------------------------------------------------- |
| 15 | + | Password Reset Controller |
| 16 | + |-------------------------------------------------------------------------- |
| 17 | + | |
| 18 | + | This controller is responsible for handling password reset emails and |
| 19 | + | includes a trait which assists in sending these notifications from |
| 20 | + | your application to your users. Feel free to explore this trait. |
| 21 | + | |
| 22 | + */ |
| 23 | + |
| 24 | + use SendsPasswordResetEmails; |
| 25 | + |
| 26 | + /** |
| 27 | + * Send a reset link to the given user. |
| 28 | + * |
| 29 | + * @param \Illuminate\Http\Request $request |
| 30 | + * @return \Illuminate\Http\RedirectResponse |
| 31 | + */ |
| 32 | + public function sendResetLinkEmail(Request $request) |
| 33 | + { |
| 34 | + $this->validate($request, ['email' => 'required|email']); |
| 35 | + |
| 36 | + // We will send the password reset link to this user. Once we have attempted |
| 37 | + // to send the link, we will examine the response then see the message we |
| 38 | + // need to show to the user. Finally, we'll send out a proper response. |
| 39 | + $response = $this->broker()->sendResetLink( |
| 40 | + $request->only('email') |
| 41 | + ); |
| 42 | + |
| 43 | + return $response == Password::RESET_LINK_SENT |
| 44 | + ? $this->sendResetLinkResponse($request, $response) |
| 45 | + : $this->sendResetLinkFailedResponse($request, $response); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Get the response for a successful password reset link. |
| 50 | + * |
| 51 | + * @param string $response |
| 52 | + * @return mixed |
| 53 | + */ |
| 54 | + protected function sendResetLinkResponse(Request $request, $response) |
| 55 | + { |
| 56 | + if ($request->expectsJson()) { |
| 57 | + return response()->json([ |
| 58 | + 'status' => trans($response) |
| 59 | + ]); |
| 60 | + } |
| 61 | + return back()->with('status', trans($response)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get the response for a failed password reset link. |
| 66 | + * |
| 67 | + * @param Request $request |
| 68 | + * @param $response |
| 69 | + * @return mixed |
| 70 | + */ |
| 71 | + protected function sendResetLinkFailedResponse(Request $request, $response) |
| 72 | + { |
| 73 | + if ($request->expectsJson()) { |
| 74 | + return new JsonResponse(['email' => trans($response) ], 422); |
| 75 | + } |
| 76 | + return back()->withErrors( |
| 77 | + ['email' => trans($response)] |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Display the form to request a password reset link. |
| 83 | + * |
| 84 | + * @return \Illuminate\Http\Response |
| 85 | + */ |
| 86 | + public function showLinkRequestForm() |
| 87 | + { |
| 88 | + return view('auth.passwords.email'); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Create a new controller instance. |
| 93 | + * |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function __construct() |
| 97 | + { |
| 98 | + $this->middleware('guest'); |
| 99 | + } |
| 100 | +} |
0 commit comments