AuthController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php namespace App\Http\Controllers\Auth;
  2. use App\User;
  3. use Illuminate\Http\Request;
  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 Request $request
  37. * @return Response
  38. */
  39. public function postRegister(Request $request)
  40. {
  41. $this->validate($request, [
  42. 'name' => 'required|max:255',
  43. 'email' => 'required|email|max:255|unique:users',
  44. 'password' => 'required|min:6|confirmed',
  45. ]);
  46. $user = User::forceCreate([
  47. 'name' => $request->name,
  48. 'email' => $request->email,
  49. 'password' => bcrypt($request->password),
  50. ]);
  51. $this->auth->login($user);
  52. return redirect('/home');
  53. }
  54. /**
  55. * Show the application login form.
  56. *
  57. * @return Response
  58. */
  59. public function getLogin()
  60. {
  61. return view('auth.login');
  62. }
  63. /**
  64. * Handle a login request to the application.
  65. *
  66. * @param Request $request
  67. * @return Response
  68. */
  69. public function postLogin(Request $request)
  70. {
  71. $this->validate($request, [
  72. 'email' => 'required', 'password' => 'required'
  73. ]);
  74. $credentials = $request->only('email', 'password');
  75. if ($this->auth->attempt($credentials, $request->has('remember')))
  76. {
  77. return redirect('/home');
  78. }
  79. return redirect('/auth/login')
  80. ->withInput($request->only('email'))
  81. ->withErrors([
  82. 'email' => 'These credentials do not match our records.',
  83. ]);
  84. }
  85. /**
  86. * Log the user out of the application.
  87. *
  88. * @return Response
  89. */
  90. public function getLogout()
  91. {
  92. $this->auth->logout();
  93. return redirect('/');
  94. }
  95. }