123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Filament\Pages\Auth;
  3. use Filament\Forms\Components\ViewField;
  4. use Filament\Http\Responses\Auth\LoginResponse;
  5. use Filament\Notifications\Notification;
  6. use Filament\Pages\Auth\Login as BaseLogin;
  7. use Filament\Actions\Action;
  8. use Filament\Actions\ActionGroup;
  9. use Filament\Forms\Components\TextInput;
  10. use Filament\Forms\Components\Component;
  11. use App\Service\CaptchaService;
  12. use Filament\Forms\Form;
  13. use Illuminate\Validation\ValidationException;
  14. use Livewire\Attributes\Computed;
  15. use Illuminate\Support\Facades\Cache;
  16. use Filament\Forms\Components\View;
  17. class Login extends BaseLogin
  18. {
  19. protected static ?string $navigationIcon = 'heroicon-o-document-text';
  20. protected static string $view = 'filament.pages.auth.login';
  21. public ?string $verification_code = null;
  22. public ?string $captchaImage = null;
  23. public function mount(): void
  24. {
  25. parent::mount();
  26. if (!$this->captchaImage) {
  27. $this->refreshCaptcha();
  28. }
  29. }
  30. public function refreshCaptcha(): void
  31. {
  32. $captchaService = app(CaptchaService::class);
  33. $code = $captchaService->generateCode();
  34. // Store code in cache with user's session ID as key
  35. Cache::put(
  36. 'verification_code_' . session()->getId(),
  37. $code,
  38. now()->addMinutes(5)
  39. );
  40. $this->captchaImage = $captchaService->generateImage($code);
  41. $this->dispatch('captcha-refreshed');
  42. }
  43. public function captchaImageUrl(): string
  44. {
  45. if(!$this->captchaImage)$this->refreshCaptcha();
  46. return $this->captchaImage;
  47. }
  48. public function form(Form $form): Form
  49. {
  50. return $form
  51. ->schema([
  52. $this->getEmailFormComponent(),
  53. $this->getPasswordFormComponent(),
  54. $this->getVerificationCodeFormComponent(),
  55. ViewField::make('captcha')
  56. ->view('filament.pages.auth.captcha'),
  57. $this->getRememberFormComponent(),
  58. ])
  59. ->statePath('data');
  60. }
  61. protected function getVerificationCodeFormComponent(): Component
  62. {
  63. return TextInput::make('verification_code')
  64. ->label('Verification Code')
  65. ->required()
  66. ->length(6)
  67. ->placeholder('Enter code shown above');
  68. }
  69. public function authenticate(): ?LoginResponse
  70. {
  71. $this->validate();
  72. $formData = $this->form->getState();
  73. if (! $this->verifyCode($formData['verification_code'])) {
  74. throw ValidationException::withMessages([
  75. 'data.verification_code' => __('驗證碼錯誤'),
  76. ]);
  77. }
  78. try {
  79. return parent::authenticate();
  80. } catch (ValidationException $e) {
  81. $this->refreshCaptcha();
  82. // throw $e;
  83. throw ValidationException::withMessages([
  84. 'verification_code' => __('Invalid verification code.'),
  85. ]);
  86. }
  87. }
  88. protected function verifyCode(string $code): bool
  89. {
  90. $validCode = Cache::get('verification_code_' . session()->getId());
  91. return $code === $validCode;
  92. }
  93. }