AuthController.php 1.8 KB

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