From df384dd88d51c43e09bd838b0fec00e56e872430 Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 1 Jun 2018 01:03:34 +0100 Subject: [PATCH 1/4] Add confirmed method, called on confirmation. --- README.md | 10 ++++++++++ src/Traits/RegistersUsers.php | 14 +++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8421725..52bdc50 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,16 @@ This package comes with a language file, that allows you to modify the error / c might see. In addition to that, you can change the notification class that will be used to send the confirmation code completely, by changing it in the `config/confirmation.php` file. +### Automatically logging users in, or adding custom logic, on confirmation +On successful email confirmation, this package calls a `confirmed` function, which you can override in order to add any custom logic, such as sending a welcome email or automatically logging the user in. + +For example, if you want to automatically log the user in on confirmation, inside your `RegisterController` simply add: +```php +public function confirmed($user) { + $this->guard()->login($user); +} +``` + ### Testing ``` bash diff --git a/src/Traits/RegistersUsers.php b/src/Traits/RegistersUsers.php index 0b2907d..e2a22b0 100644 --- a/src/Traits/RegistersUsers.php +++ b/src/Traits/RegistersUsers.php @@ -27,7 +27,8 @@ public function confirm($confirmation_code) $user->confirmed_at = now(); $user->save(); - return redirect(route('login'))->with('confirmation', __('confirmation::confirmation.confirmation_successful')); + return $this->confirmed($user) + ?: redirect(route('login'))->with('confirmation', __('confirmation::confirmation.confirmation_successful')); } /** @@ -79,4 +80,15 @@ protected function sendConfirmationToUser($user) $notification = app(config('confirmation.notification')); $user->notify($notification); } + + + /** + * The users email address has been confirmed. + * + * @param mixed $user + * @return mixed + */ + protected function confirmed($user) { + // + } } \ No newline at end of file From ad3cdefbcc51b57965a0dffefd07615382c5005a Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 1 Jun 2018 22:49:57 +0100 Subject: [PATCH 2/4] Add Confirmed event --- src/EmailConfirmationServiceProvider.php | 2 -- src/Events/Confirmed.php | 32 ++++++++++++++++++++++++ src/Traits/RegistersUsers.php | 3 +++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/Events/Confirmed.php diff --git a/src/EmailConfirmationServiceProvider.php b/src/EmailConfirmationServiceProvider.php index e3cef10..13efb6c 100644 --- a/src/EmailConfirmationServiceProvider.php +++ b/src/EmailConfirmationServiceProvider.php @@ -2,9 +2,7 @@ namespace BeyondCode\EmailConfirmation; -use Illuminate\Auth\Events\Registered; use Illuminate\Support\ServiceProvider; -use BeyondCode\EmailConfirmation\Listeners\CreateConfirmationCode; class EmailConfirmationServiceProvider extends ServiceProvider { diff --git a/src/Events/Confirmed.php b/src/Events/Confirmed.php new file mode 100644 index 0000000..4e890a1 --- /dev/null +++ b/src/Events/Confirmed.php @@ -0,0 +1,32 @@ +user = $user; + } +} \ No newline at end of file diff --git a/src/Traits/RegistersUsers.php b/src/Traits/RegistersUsers.php index e2a22b0..8dd5f40 100644 --- a/src/Traits/RegistersUsers.php +++ b/src/Traits/RegistersUsers.php @@ -2,6 +2,7 @@ namespace BeyondCode\EmailConfirmation\Traits; +use BeyondCode\EmailConfirmation\Events\Confirmed; use Illuminate\Http\Request; use Illuminate\Auth\Events\Registered; @@ -27,6 +28,8 @@ public function confirm($confirmation_code) $user->confirmed_at = now(); $user->save(); + event(new Confirmed($user)); + return $this->confirmed($user) ?: redirect(route('login'))->with('confirmation', __('confirmation::confirmation.confirmation_successful')); } From 9619dde925239b059cd5cc24fd3a315023fe3cb1 Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 1 Jun 2018 23:08:48 +0100 Subject: [PATCH 3/4] confirmed event tests --- tests/ConfirmationTest.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/ConfirmationTest.php b/tests/ConfirmationTest.php index 9d926b3..6dc33d6 100644 --- a/tests/ConfirmationTest.php +++ b/tests/ConfirmationTest.php @@ -2,7 +2,9 @@ namespace BeyondCode\EmailConfirmation\Tests; +use BeyondCode\EmailConfirmation\Events\Confirmed; use Illuminate\Auth\Notifications\ResetPassword; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Notification; use BeyondCode\EmailConfirmation\Tests\Models\User; use BeyondCode\EmailConfirmation\Notifications\ConfirmEmail; @@ -139,4 +141,37 @@ public function it_does_not_allow_reset_password_request_for_unconfirmed_users() Notification::assertNotSentTo($user, ResetPassword::class); } + + /** @test */ + public function it_dispatches_confirmed_event_on_successful_confirmation() + { + Event::fake(); + + $user = User::create([ + 'email' => 'marcel@beyondco.de', + 'password' => bcrypt('test123'), + 'confirmed_at' => null, + 'confirmation_code' => 'abcdefg' + ]); + + $response = $this->get('/register/confirm/abcdefg'); + + Event::assertDispatched(Confirmed::class, function ($e) use ($user) { + return $e->user->email === $user->email; + }); + + $response->assertRedirect('/login'); + } + + /** @test */ + public function it_does_not_dispatch_confirmed_event_on_failed_confirmation() + { + Event::fake(); + + $response = $this->get('/register/confirm/foo'); + + Event::assertNotDispatched(Confirmed::class); + + $response->assertStatus(404); + } } From 5870c4e5c1c73fa4b47684c27472522d44935178 Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 1 Jun 2018 23:31:22 +0100 Subject: [PATCH 4/4] update README --- README.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 52bdc50..e1e4a93 100644 --- a/README.md +++ b/README.md @@ -74,16 +74,27 @@ This package comes with a language file, that allows you to modify the error / c might see. In addition to that, you can change the notification class that will be used to send the confirmation code completely, by changing it in the `config/confirmation.php` file. -### Automatically logging users in, or adding custom logic, on confirmation -On successful email confirmation, this package calls a `confirmed` function, which you can override in order to add any custom logic, such as sending a welcome email or automatically logging the user in. +### The Confirmed Event +On successful email confirmation, this package dispatches a `Confirmed` event, in order for you to conveniently handle +any custom logic, such as sending a welcome email or automatically logging the user in. + +Simply add the `Confirmed` event, and your listeners, to the `EventServiceProvider` in your application: -For example, if you want to automatically log the user in on confirmation, inside your `RegisterController` simply add: ```php -public function confirmed($user) { - $this->guard()->login($user); -} + /** + * The event listener mappings for the application. + * + * @var array + */ + protected $listen = [ + 'BeyondCode\EmailConfirmation\Events\Confirmed' => [ + 'App\Listeners\YourOnConfirmedListener' + ] + ]; ``` +For more information about registering events and listeners, please refer to the [Laravel docs](https://laravel.com/docs/events#registering-events-and-listeners). + ### Testing ``` bash