PasswordController.php 2.1 KB

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