]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/PasswordController.php
Improved password reset flow with notifications.
[bookstack] / app / Http / Controllers / Auth / PasswordController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Http\Controllers\Controller;
6 use Illuminate\Foundation\Auth\ResetsPasswords;
7 use Illuminate\Http\Request;
8 use Password;
9
10 class PasswordController extends Controller
11 {
12     /*
13     |--------------------------------------------------------------------------
14     | Password Reset Controller
15     |--------------------------------------------------------------------------
16     |
17     | This controller is responsible for handling password reset requests
18     | and uses a simple trait to include this behavior. You're free to
19     | explore this trait and override any methods you wish to tweak.
20     |
21     */
22
23     use ResetsPasswords;
24
25     protected $redirectTo = '/';
26
27     /**
28      * Create a new password controller instance.
29      */
30     public function __construct()
31     {
32         $this->middleware('guest');
33     }
34
35
36     /**
37      * Send a reset link to the given user.
38      *
39      * @param  \Illuminate\Http\Request  $request
40      * @return \Illuminate\Http\Response
41      */
42     public function sendResetLinkEmail(Request $request)
43     {
44         $this->validate($request, ['email' => 'required|email']);
45
46         $broker = $this->getBroker();
47
48         $response = Password::broker($broker)->sendResetLink(
49             $request->only('email'), $this->resetEmailBuilder()
50         );
51
52         switch ($response) {
53             case Password::RESET_LINK_SENT:
54                 $message = 'A password reset link has been sent to ' . $request->get('email') . '.';
55                 session()->flash('success', $message);
56                 return $this->getSendResetLinkEmailSuccessResponse($response);
57
58             case Password::INVALID_USER:
59             default:
60                 return $this->getSendResetLinkEmailFailureResponse($response);
61         }
62     }
63
64     /**
65      * Get the response for after a successful password reset.
66      *
67      * @param  string  $response
68      * @return \Symfony\Component\HttpFoundation\Response
69      */
70     protected function getResetSuccessResponse($response)
71     {
72         $message = 'Your password has been successfully reset.';
73         session()->flash('success', $message);
74         return redirect($this->redirectPath())->with('status', trans($response));
75     }
76 }