|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Services\Concerns; |
| 4 | + |
| 5 | +use Illuminate\Auth\Events\PasswordReset; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Illuminate\Support\Facades\Auth; |
| 8 | +use Illuminate\Support\Facades\Hash; |
| 9 | +use Illuminate\Support\Facades\Password; |
| 10 | +use Illuminate\Support\Str; |
| 11 | + |
| 12 | +trait ResetsPasswords |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Get the post register / login redirect path. |
| 16 | + * |
| 17 | + * @return string |
| 18 | + */ |
| 19 | + public function redirectPath() |
| 20 | + { |
| 21 | + if (method_exists($this, 'redirectTo')) { |
| 22 | + return $this->redirectTo(); |
| 23 | + } |
| 24 | + |
| 25 | + return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Display the password reset view for the given token. |
| 30 | + * |
| 31 | + * If no token is present, display the link request form. |
| 32 | + * |
| 33 | + * @param \Illuminate\Http\Request $request |
| 34 | + * @param string|null $token |
| 35 | + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
| 36 | + */ |
| 37 | + public function showResetForm(Request $request, $token = null) |
| 38 | + { |
| 39 | + return view('auth.passwords.reset')->with( |
| 40 | + ['token' => $token, 'email' => $request->email] |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Reset the given user's password. |
| 46 | + * |
| 47 | + * @param \Illuminate\Http\Request $request |
| 48 | + * @return \Illuminate\Http\RedirectResponse |
| 49 | + */ |
| 50 | + public function reset(Request $request) |
| 51 | + { |
| 52 | + $request->validate($this->rules(), $this->validationErrorMessages()); |
| 53 | + |
| 54 | + // Here we will attempt to reset the user's password. If it is successful we |
| 55 | + // will update the password on an actual user model and persist it to the |
| 56 | + // database. Otherwise we will parse the error and return the response. |
| 57 | + $response = $this->broker()->reset( |
| 58 | + $this->credentials($request), function ($user, $password) { |
| 59 | + $this->resetPassword($user, $password); |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + // If the password was successfully reset, we will redirect the user back to |
| 64 | + // the application's home authenticated view. If there is an error we can |
| 65 | + // redirect them back to where they came from with their error message. |
| 66 | + return $response == Password::PASSWORD_RESET |
| 67 | + ? $this->sendResetResponse($response) |
| 68 | + : $this->sendResetFailedResponse($request, $response); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get the password reset validation rules. |
| 73 | + * |
| 74 | + * @return array |
| 75 | + */ |
| 76 | + protected function rules() |
| 77 | + { |
| 78 | + return [ |
| 79 | + 'token' => 'required', |
| 80 | + 'email' => 'required|email', |
| 81 | + 'password' => 'required|confirmed|min:6', |
| 82 | + ]; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the password reset validation error messages. |
| 87 | + * |
| 88 | + * @return array |
| 89 | + */ |
| 90 | + protected function validationErrorMessages() |
| 91 | + { |
| 92 | + return []; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get the password reset credentials from the request. |
| 97 | + * |
| 98 | + * @param \Illuminate\Http\Request $request |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + protected function credentials(Request $request) |
| 102 | + { |
| 103 | + return $request->only( |
| 104 | + 'email', 'password', 'password_confirmation', 'token' |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Reset the given user's password. |
| 110 | + * |
| 111 | + * @param \Illuminate\Contracts\Auth\CanResetPassword $user |
| 112 | + * @param string $password |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + protected function resetPassword($user, $password) |
| 116 | + { |
| 117 | + $user->password = Hash::make($password); |
| 118 | + |
| 119 | + $user->setRememberToken(Str::random(60)); |
| 120 | + |
| 121 | + $user->save(); |
| 122 | + |
| 123 | + event(new PasswordReset($user)); |
| 124 | + |
| 125 | + $this->guard()->login($user); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Get the response for a successful password reset. |
| 130 | + * |
| 131 | + * @param string $response |
| 132 | + * @return \Illuminate\Http\RedirectResponse |
| 133 | + */ |
| 134 | + protected function sendResetResponse($response) |
| 135 | + { |
| 136 | + return redirect($this->redirectPath()) |
| 137 | + ->with('status', trans($response)); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get the response for a failed password reset. |
| 142 | + * |
| 143 | + * @param \Illuminate\Http\Request |
| 144 | + * @param string $response |
| 145 | + * @return \Illuminate\Http\RedirectResponse |
| 146 | + */ |
| 147 | + protected function sendResetFailedResponse(Request $request, $response) |
| 148 | + { |
| 149 | + return redirect()->back() |
| 150 | + ->withInput($request->only('email')) |
| 151 | + ->withErrors(['email' => trans($response)]); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get the broker to be used during password reset. |
| 156 | + * |
| 157 | + * @return \Illuminate\Contracts\Auth\PasswordBroker |
| 158 | + */ |
| 159 | + public function broker() |
| 160 | + { |
| 161 | + return Password::broker(); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get the guard to be used during password reset. |
| 166 | + * |
| 167 | + * @return \Illuminate\Contracts\Auth\StatefulGuard |
| 168 | + */ |
| 169 | + protected function guard() |
| 170 | + { |
| 171 | + return Auth::guard(); |
| 172 | + } |
| 173 | +} |
0 commit comments