Skip to content

Commit

Permalink
Merge pull request #14 from rickwest/confirmed
Browse files Browse the repository at this point in the history
Dispatch a Confirmed event and confirmed method on successful confirmation.
  • Loading branch information
mpociot authored Jun 4, 2018
2 parents b1a4038 + 5870c4e commit 6e9e84d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +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.

### 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:

```php
/**
* 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
Expand Down
2 changes: 0 additions & 2 deletions src/EmailConfirmationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
32 changes: 32 additions & 0 deletions src/Events/Confirmed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace BeyondCode\EmailConfirmation\Events;

use Illuminate\Queue\SerializesModels;

/**
* Class Confirmed
* @package BeyondCode\EmailConfirmation\Events
*/
class Confirmed
{
use SerializesModels;

/**
* The authenticated user.
*
* @var \Illuminate\Contracts\Auth\Authenticatable
*/
public $user;

/**
* Create a new event instance.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}
}
17 changes: 16 additions & 1 deletion src/Traits/RegistersUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BeyondCode\EmailConfirmation\Traits;

use BeyondCode\EmailConfirmation\Events\Confirmed;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;

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

return redirect(route('login'))->with('confirmation', __('confirmation::confirmation.confirmation_successful'));
event(new Confirmed($user));

return $this->confirmed($user)
?: redirect(route('login'))->with('confirmation', __('confirmation::confirmation.confirmation_successful'));
}

/**
Expand Down Expand Up @@ -79,4 +83,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) {
//
}
}
35 changes: 35 additions & 0 deletions tests/ConfirmationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -141,4 +143,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' => '[email protected]',
'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);
}
}

0 comments on commit 6e9e84d

Please sign in to comment.