AuthController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. class AuthController extends Controller {
  7. /**
  8. * The Guard implementation.
  9. *
  10. * @var Guard
  11. */
  12. protected $auth;
  13. /**
  14. * Create a new authentication controller instance.
  15. *
  16. * @param Guard $auth
  17. * @return void
  18. */
  19. public function __construct(Guard $auth)
  20. {
  21. $this->auth = $auth;
  22. $this->middleware('guest', ['except' => 'getLogout']);
  23. }
  24. /**
  25. * Show the application registration form.
  26. *
  27. * @return Response
  28. */
  29. public function getRegister()
  30. {
  31. return view('auth.register');
  32. }
  33. /**
  34. * Handle a registration request for the application.
  35. *
  36. * @param RegisterRequest $request
  37. * @return Response
  38. */
  39. public function postRegister(Requests\Auth\RegisterRequest $request)
  40. {
  41. $user = User::forceCreate([
  42. 'name' => $request->name,
  43. 'email' => $request->email,
  44. 'password' => bcrypt($request->password),
  45. ]);
  46. $this->auth->login($user);
  47. return redirect('/home');
  48. }
  49. /**
  50. * Show the application login form.
  51. *
  52. * @return Response
  53. */
  54. public function getLogin()
  55. {
  56. return view('auth.login');
  57. }
  58. /**
  59. * Handle a login request to the application.
  60. *
  61. * @param LoginRequest $request
  62. * @return Response
  63. */
  64. public function postLogin(Requests\Auth\LoginRequest $request)
  65. {
  66. $credentials = $request->only('email', 'password');
  67. if ($this->auth->attempt($credentials, $request->has('remember')))
  68. {
  69. return redirect('/home');
  70. }
  71. return redirect('/auth/login')
  72. ->withInput($request->only('email'))
  73. ->withErrors([
  74. 'email' => 'These credentials do not match our records.',
  75. ]);
  76. }
  77. /**
  78. * Log the user out of the application.
  79. *
  80. * @return Response
  81. */
  82. public function getLogout()
  83. {
  84. $this->auth->logout();
  85. return redirect('/');
  86. }
  87. }