send verification email after registering

This commit is contained in:
Pig Fang 2020-05-27 16:37:55 +08:00
parent 975464efb6
commit d2d549b657
3 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace App\Listeners;
use App\Mail\EmailVerification;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\URL;
class SendEmailVerification
{
public function handle(User $user)
{
if (option('require_verification')) {
$url = URL::signedRoute('auth.verify', ['uid' => $user->uid], null, false);
try {
Mail::to($user->email)->send(new EmailVerification(url($url)));
} catch (\Exception $e) {
report($e);
}
}
}
}

View File

@ -36,5 +36,8 @@ class EventServiceProvider extends ServiceProvider
'player.name.updated' => [
Listeners\SinglePlayer\UpdateOwnerNickName::class,
],
'auth.registration.completed' => [
Listeners\SendEmailVerification::class,
],
];
}

View File

@ -0,0 +1,31 @@
<?php
namespace Tests;
use App\Mail\EmailVerification;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
class SendEmailVerificationTest extends TestCase
{
use DatabaseTransactions;
public function testHandle()
{
Mail::fake();
option(['require_verification' => true]);
$user = factory(User::class)->create(['verified' => false]);
Event::dispatch('auth.registration.completed', [$user]);
Mail::assertSent(EmailVerification::class, function ($mail) use ($user) {
return $mail->hasTo($user->email);
});
Mail::shouldReceive('to')
->once()
->andThrow(new \Mockery\Exception('A fake exception.'));
Event::dispatch('auth.registration.completed', [$user]);
}
}