PasswordController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php namespace App\Http\Controllers\Auth;
  2. use App\User;
  3. use App\Http\Requests;
  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 EmailPasswordLinkRequest $request
  46. * @return Response
  47. */
  48. public function postEmail(Requests\Auth\EmailPasswordLinkRequest $request)
  49. {
  50. switch ($response = $this->passwords->sendResetLink($request->only('email')))
  51. {
  52. case PasswordBroker::INVALID_USER:
  53. return redirect()->back()->withErrors(['email' =>trans($response)]);
  54. case PasswordBroker::RESET_LINK_SENT:
  55. return redirect()->back()->with('status', trans($response));
  56. }
  57. }
  58. /**
  59. * Display the password reset view for the given token.
  60. *
  61. * @param string $token
  62. * @return Response
  63. */
  64. public function getReset($token = null)
  65. {
  66. if (is_null($token))
  67. {
  68. throw new NotFoundHttpException;
  69. }
  70. return view('auth.reset')->with('token', $token);
  71. }
  72. /**
  73. * Reset the given user's password.
  74. *
  75. * @param ResetPasswordRequest $request
  76. * @return Response
  77. */
  78. public function postReset(Requests\Auth\ResetPasswordRequest $request)
  79. {
  80. $credentials = $request->only(
  81. 'email', 'password', 'password_confirmation', 'token'
  82. );
  83. $response = $this->passwords->reset($credentials, function($user, $password)
  84. {
  85. $user->password = bcrypt($password);
  86. $user->save();
  87. });
  88. switch ($response)
  89. {
  90. case PasswordBroker::INVALID_PASSWORD:
  91. case PasswordBroker::INVALID_TOKEN:
  92. case PasswordBroker::INVALID_USER:
  93. return redirect()->back()
  94. ->withInput($request->only('email'))
  95. ->withErrors(['email' => trans($response)]);
  96. case PasswordBroker::PASSWORD_RESET:
  97. return $this->loginAndRedirect($request->email);
  98. }
  99. }
  100. /**
  101. * Login the user with the given e-mail address and redirect home.
  102. *
  103. * @param string $email
  104. * @return Response
  105. */
  106. protected function loginAndRedirect($email)
  107. {
  108. $this->auth->login(User::where('email', $email)->firstOrFail());
  109. return redirect('/home');
  110. }
  111. }