104 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			104 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Http\Controllers\Auth;
 | |
| 
 | |
| use App\Abstracts\Http\Controller;
 | |
| use Illuminate\Foundation\Auth\ResetsPasswords;
 | |
| use Illuminate\Http\Request;
 | |
| use Illuminate\Support\Facades\Password;
 | |
| use Illuminate\Support\Str;
 | |
| 
 | |
| class Reset extends Controller
 | |
| {
 | |
|     use ResetsPasswords;
 | |
| 
 | |
|     /**
 | |
|      * Where to redirect users after resetting their password.
 | |
|      *
 | |
|      * @var string
 | |
|      */
 | |
|     public $redirectTo = '/';
 | |
| 
 | |
|     /**
 | |
|      * Create a new controller instance.
 | |
|      *
 | |
|      * @return void
 | |
|      */
 | |
|     public function __construct()
 | |
|     {
 | |
|         $this->middleware('guest');
 | |
|     }
 | |
| 
 | |
|     public function create(Request $request, $token = null)
 | |
|     {
 | |
|         return view('auth.reset.create')->with(
 | |
|             ['token' => $token, 'email' => $request->email]
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     public function store(Request $request)
 | |
|     {
 | |
|         $this->validate($request, $this->rules(), $this->validationErrorMessages());
 | |
| 
 | |
|         // Here we will attempt to reset the user's password. If it is successful we
 | |
|         // will update the password on an actual user model and persist it to the
 | |
|         // database. Otherwise we will parse the error and return the response.
 | |
|         $response = $this->broker()->reset(
 | |
|             $this->credentials($request), function ($user, $password) {
 | |
|                 $this->resetPassword($user, $password);
 | |
|             }
 | |
|         );
 | |
| 
 | |
|         // If the password was successfully reset, we will redirect the user back to
 | |
|         // the application's home authenticated view. If there is an error we can
 | |
|         // redirect them back to where they came from with their error message.
 | |
|         return $response == Password::PASSWORD_RESET
 | |
|             ? $this->sendResetResponse($response)
 | |
|             : $this->sendResetFailedResponse($request, $response);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Reset the given user's password.
 | |
|      *
 | |
|      * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
 | |
|      * @param  string  $password
 | |
|      * @return void
 | |
|      */
 | |
|     protected function resetPassword($user, $password)
 | |
|     {
 | |
|         $user->forceFill([
 | |
|             'password' => $password,
 | |
|             'remember_token' => Str::random(60),
 | |
|         ])->save();
 | |
| 
 | |
|         $this->guard()->login($user);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Get the response for a successful password reset.
 | |
|      *
 | |
|      * @param  string  $response
 | |
|      * @return \Illuminate\Http\RedirectResponse
 | |
|      */
 | |
|     protected function sendResetResponse($response)
 | |
|     {
 | |
|         flash(trans($response))->success();
 | |
| 
 | |
|         return redirect($this->redirectTo);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Get the response for a failed password reset.
 | |
|      *
 | |
|      * @param  \Illuminate\Http\Request
 | |
|      * @param  string  $response
 | |
|      * @return \Illuminate\Http\RedirectResponse
 | |
|      */
 | |
|     protected function sendResetFailedResponse(Request $request, $response)
 | |
|     {
 | |
|         return redirect()->back()
 | |
|             ->withInput($request->only('email'))
 | |
|             ->withErrors(['email' => trans($response)]);
 | |
|     }
 | |
| }
 |