Skip to content

Commit

Permalink
Merge pull request #19 from aryehraber/feat/support-login
Browse files Browse the repository at this point in the history
Support Login event
  • Loading branch information
aryehraber authored May 4, 2021
2 parents e490a09 + 382390e commit ee768da
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"name": "Captcha",
"slug": "captcha",
"description": "Protect your Statamic forms using a Captcha service",
"version": "1.4.0"
"version": "1.5.0"
},
"laravel": {
"providers": [
Expand Down
1 change: 1 addition & 0 deletions config/captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'secret' => env('CAPTCHA_SECRET', ''),
'collections' => [],
'forms' => [],
'user_login' => false,
'user_registration' => false,
'error_message' => 'Captcha failed.',
'disclaimer' => '',
Expand Down
3 changes: 3 additions & 0 deletions src/CaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use AryehRaber\Captcha\Listeners\ValidateEntry;
use AryehRaber\Captcha\Listeners\ValidateFormSubmission;
use AryehRaber\Captcha\Listeners\ValidateUserLogin;
use AryehRaber\Captcha\Listeners\ValidateUserRegistration;
use Illuminate\Auth\Events\Login;
use Statamic\Events\EntrySaving;
use Statamic\Events\FormSubmitted;
use Statamic\Events\UserRegistering;
Expand All @@ -21,6 +23,7 @@ class CaptchaServiceProvider extends AddonServiceProvider
protected $listen = [
EntrySaving::class => [ValidateEntry::class],
FormSubmitted::class => [ValidateFormSubmission::class],
Login::class => [ValidateUserLogin::class],
UserRegistering::class => [ValidateUserRegistration::class],
];

Expand Down
37 changes: 37 additions & 0 deletions src/Listeners/ValidateUserLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace AryehRaber\Captcha\Listeners;

use AryehRaber\Captcha\Captcha;
use Illuminate\Auth\Events\Login;
use Illuminate\Validation\ValidationException;

class ValidateUserLogin
{
protected $captcha;

public function __construct(Captcha $captcha)
{
$this->captcha = $captcha;
}

public function handle(Login $event)
{
$user = $event->user;

if (! $this->shouldVerify()) {
return $user;
}

if ($this->captcha->verify()->invalidResponse()) {
throw ValidationException::withMessages(['captcha' => config('captcha.error_message')]);
}

return $user;
}

protected function shouldVerify()
{
return config('captcha.user_login', false);
}
}

0 comments on commit ee768da

Please sign in to comment.