25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers;
  4. use Core\Controller;
  5. use Core\Request;
  6. use Core\Response;
  7. use Cartalyst\Sentinel\Native\Facades\Sentinel;
  8. use Cartalyst\Sentinel\Checkpoints\ThrottlingException;
  9. class AuthController extends Controller
  10. {
  11. public function showLogin(): Response
  12. {
  13. if (Sentinel::check()) {
  14. return $this->redirect('/');
  15. }
  16. return $this->view('auth.login', ['pageTitle' => 'Login']);
  17. }
  18. public function login(): Response
  19. {
  20. $request = Request::capture();
  21. if (!verify_csrf_token($request->input('_token'))) {
  22. return $this->view('auth.login', [
  23. 'pageTitle' => 'Login',
  24. 'error' => 'Invalid request. Please try again.',
  25. ]);
  26. }
  27. $credentials = [
  28. 'email' => (string) $request->input('email'),
  29. 'password' => (string) $request->input('password'),
  30. ];
  31. $remember = (bool) $request->input('remember');
  32. try {
  33. if (Sentinel::authenticate($credentials, $remember)) {
  34. return $this->redirect('/');
  35. }
  36. } catch (ThrottlingException $e) {
  37. return $this->view('auth.login', [
  38. 'pageTitle' => 'Login',
  39. 'error' => 'Too many failed attempts. Please wait ' . $e->getDelay() . ' seconds.',
  40. ]);
  41. }
  42. return $this->view('auth.login', [
  43. 'pageTitle' => 'Login',
  44. 'error' => 'Invalid email or password.',
  45. ]);
  46. }
  47. public function logout(): Response
  48. {
  49. $request = Request::capture();
  50. if (!verify_csrf_token($request->input('_token'))) {
  51. return $this->redirect('/');
  52. }
  53. Sentinel::logout();
  54. return $this->redirect('/login');
  55. }
  56. }

Powered by TurnKey Linux.