PasswordController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php namespace App\Http\Controllers\Auth;
  2. use App\User;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Contracts\Auth\Guard;
  6. use Illuminate\Contracts\Auth\PasswordBroker;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. class PasswordController extends Controller {
  9. /**
  10. * The Guard implementation.
  11. *
  12. * @var Guard
  13. */
  14. protected $auth;
  15. /**
  16. * The password broker implementation.
  17. *
  18. * @var PasswordBroker
  19. */
  20. protected $passwords;
  21. /**
  22. * Create a new password controller instance.
  23. *
  24. * @param PasswordBroker $passwords
  25. * @return void
  26. */
  27. public function __construct(Guard $auth, PasswordBroker $passwords)
  28. {
  29. $this->auth = $auth;
  30. $this->passwords = $passwords;
  31. $this->middleware('guest');
  32. }
  33. /**
  34. * Display the form to request a password reset link.
  35. *
  36. * @return Response
  37. */
  38. public function getEmail()
  39. {
  40. return view('auth.password');
  41. }
  42. /**
  43. * Send a reset link to the given user.
  44. *
  45. * @param Request $request
  46. * @return Response
  47. */
  48. public function postEmail(Request $request)
  49. {
  50. $this->validate($request, ['email' => 'required']);
  51. switch ($response = $this->passwords->sendResetLink($request->only('email')))
  52. {
  53. case PasswordBroker::INVALID_USER:
  54. return redirect()->back()->withErrors(['email' =>trans($response)]);
  55. case PasswordBroker::RESET_LINK_SENT:
  56. return redirect()->back()->with('status', trans($response));
  57. }
  58. }
  59. /**
  60. * Display the password reset view for the given token.
  61. *
  62. * @param string $token
  63. * @return Response
  64. */
  65. public function getReset($token = null)
  66. {
  67. if (is_null($token))
  68. {
  69. throw new NotFoundHttpException;
  70. }
  71. return view('auth.reset')->with('token', $token);
  72. }
  73. /**
  74. * Reset the given user's password.
  75. *
  76. * @param Request $request
  77. * @return Response
  78. */
  79. public function postReset(Request $request)
  80. {
  81. $credentials = $request->only(
  82. 'email', 'password', 'password_confirmation', 'token'
  83. );
  84. $response = $this->passwords->reset($credentials, function($user, $password)
  85. {
  86. $user->password = bcrypt($password);
  87. $user->save();
  88. });
  89. switch ($response)
  90. {
  91. case PasswordBroker::INVALID_PASSWORD:
  92. case PasswordBroker::INVALID_TOKEN:
  93. case PasswordBroker::INVALID_USER:
  94. return redirect()->back()
  95. ->withInput($request->only('email'))
  96. ->withErrors(['email' => trans($response)]);
  97. case PasswordBroker::PASSWORD_RESET:
  98. return $this->loginAndRedirect($request->email);
  99. }
  100. }
  101. /**
  102. * Login the user with the given e-mail address and redirect home.
  103. *
  104. * @param string $email
  105. * @return Response
  106. */
  107. protected function loginAndRedirect($email)
  108. {
  109. $this->auth->login(User::where('email', $email)->firstOrFail());
  110. return redirect('/home');
  111. }
  112. }