Skip to content

Commit 6e9e84d

Browse files
authored
Merge pull request #14 from rickwest/confirmed
Dispatch a Confirmed event and confirmed method on successful confirmation.
2 parents b1a4038 + 5870c4e commit 6e9e84d

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ This package comes with a language file, that allows you to modify the error / c
7474
might see. In addition to that, you can change the notification class that will be used to send the confirmation code
7575
completely, by changing it in the `config/confirmation.php` file.
7676

77+
### The Confirmed Event
78+
On successful email confirmation, this package dispatches a `Confirmed` event, in order for you to conveniently handle
79+
any custom logic, such as sending a welcome email or automatically logging the user in.
80+
81+
Simply add the `Confirmed` event, and your listeners, to the `EventServiceProvider` in your application:
82+
83+
```php
84+
/**
85+
* The event listener mappings for the application.
86+
*
87+
* @var array
88+
*/
89+
protected $listen = [
90+
'BeyondCode\EmailConfirmation\Events\Confirmed' => [
91+
'App\Listeners\YourOnConfirmedListener'
92+
]
93+
];
94+
```
95+
96+
For more information about registering events and listeners, please refer to the [Laravel docs](https://laravel.com/docs/events#registering-events-and-listeners).
97+
7798
### Testing
7899

79100
``` bash

src/EmailConfirmationServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace BeyondCode\EmailConfirmation;
44

5-
use Illuminate\Auth\Events\Registered;
65
use Illuminate\Support\ServiceProvider;
7-
use BeyondCode\EmailConfirmation\Listeners\CreateConfirmationCode;
86

97
class EmailConfirmationServiceProvider extends ServiceProvider
108
{

src/Events/Confirmed.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace BeyondCode\EmailConfirmation\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
/**
8+
* Class Confirmed
9+
* @package BeyondCode\EmailConfirmation\Events
10+
*/
11+
class Confirmed
12+
{
13+
use SerializesModels;
14+
15+
/**
16+
* The authenticated user.
17+
*
18+
* @var \Illuminate\Contracts\Auth\Authenticatable
19+
*/
20+
public $user;
21+
22+
/**
23+
* Create a new event instance.
24+
*
25+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
26+
* @return void
27+
*/
28+
public function __construct($user)
29+
{
30+
$this->user = $user;
31+
}
32+
}

src/Traits/RegistersUsers.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BeyondCode\EmailConfirmation\Traits;
44

5+
use BeyondCode\EmailConfirmation\Events\Confirmed;
56
use Illuminate\Http\Request;
67
use Illuminate\Auth\Events\Registered;
78

@@ -27,7 +28,10 @@ public function confirm($confirmation_code)
2728
$user->confirmed_at = now();
2829
$user->save();
2930

30-
return redirect(route('login'))->with('confirmation', __('confirmation::confirmation.confirmation_successful'));
31+
event(new Confirmed($user));
32+
33+
return $this->confirmed($user)
34+
?: redirect(route('login'))->with('confirmation', __('confirmation::confirmation.confirmation_successful'));
3135
}
3236

3337
/**
@@ -79,4 +83,15 @@ protected function sendConfirmationToUser($user)
7983
$notification = app(config('confirmation.notification'));
8084
$user->notify($notification);
8185
}
86+
87+
88+
/**
89+
* The users email address has been confirmed.
90+
*
91+
* @param mixed $user
92+
* @return mixed
93+
*/
94+
protected function confirmed($user) {
95+
//
96+
}
8297
}

tests/ConfirmationTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace BeyondCode\EmailConfirmation\Tests;
44

5+
use BeyondCode\EmailConfirmation\Events\Confirmed;
56
use Illuminate\Auth\Notifications\ResetPassword;
7+
use Illuminate\Support\Facades\Event;
68
use Illuminate\Support\Facades\Notification;
79
use BeyondCode\EmailConfirmation\Tests\Models\User;
810
use BeyondCode\EmailConfirmation\Notifications\ConfirmEmail;
@@ -141,4 +143,37 @@ public function it_does_not_allow_reset_password_request_for_unconfirmed_users()
141143

142144
Notification::assertNotSentTo($user, ResetPassword::class);
143145
}
146+
147+
/** @test */
148+
public function it_dispatches_confirmed_event_on_successful_confirmation()
149+
{
150+
Event::fake();
151+
152+
$user = User::create([
153+
'email' => '[email protected]',
154+
'password' => bcrypt('test123'),
155+
'confirmed_at' => null,
156+
'confirmation_code' => 'abcdefg'
157+
]);
158+
159+
$response = $this->get('/register/confirm/abcdefg');
160+
161+
Event::assertDispatched(Confirmed::class, function ($e) use ($user) {
162+
return $e->user->email === $user->email;
163+
});
164+
165+
$response->assertRedirect('/login');
166+
}
167+
168+
/** @test */
169+
public function it_does_not_dispatch_confirmed_event_on_failed_confirmation()
170+
{
171+
Event::fake();
172+
173+
$response = $this->get('/register/confirm/foo');
174+
175+
Event::assertNotDispatched(Confirmed::class);
176+
177+
$response->assertStatus(404);
178+
}
144179
}

0 commit comments

Comments
 (0)