AuthController.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php namespace App\Http\Controllers\Auth;
  2. use Illuminate\Contracts\Auth\Guard;
  3. use App\Http\Requests\Auth\LoginRequest;
  4. use App\Http\Requests\Auth\RegisterRequest;
  5. /**
  6. * @Middleware("guest", except={"logout"})
  7. */
  8. class AuthController {
  9. /**
  10. * The Guard implementation.
  11. *
  12. * @var Guard
  13. */
  14. protected $auth;
  15. /**
  16. * Create a new authentication controller instance.
  17. *
  18. * @param Guard $auth
  19. * @return void
  20. */
  21. public function __construct(Guard $auth)
  22. {
  23. $this->auth = $auth;
  24. }
  25. /**
  26. * Show the application registration form.
  27. *
  28. * @Get("auth/register")
  29. *
  30. * @return Response
  31. */
  32. public function showRegistrationForm()
  33. {
  34. return view('auth.register');
  35. }
  36. /**
  37. * Handle a registration request for the application.
  38. *
  39. * @Post("auth/register")
  40. *
  41. * @param RegisterRequest $request
  42. * @return Response
  43. */
  44. public function register(RegisterRequest $request)
  45. {
  46. // Registration form is valid, create user...
  47. $this->auth->login($user);
  48. return redirect('/');
  49. }
  50. /**
  51. * Show the application login form.
  52. *
  53. * @Get("auth/login")
  54. *
  55. * @return Response
  56. */
  57. public function showLoginForm()
  58. {
  59. return view('auth.login');
  60. }
  61. /**
  62. * Handle a login request to the application.
  63. *
  64. * @Post("auth/login")
  65. *
  66. * @param LoginRequest $request
  67. * @return Response
  68. */
  69. public function login(LoginRequest $request)
  70. {
  71. if ($this->auth->attempt($request->only('email', 'password')))
  72. {
  73. return redirect('/');
  74. }
  75. return redirect('/auth/login')->withErrors([
  76. 'email' => 'These credentials do not match our records.',
  77. ]);
  78. }
  79. /**
  80. * Log the user out of the application.
  81. *
  82. * @Get("auth/logout")
  83. *
  84. * @return Response
  85. */
  86. public function logout()
  87. {
  88. $this->auth->logout();
  89. return redirect('/');
  90. }
  91. }