Skip to content

Commit 3a0e444

Browse files
committed
Resend verification email when email changes
1 parent e87a25c commit 3a0e444

File tree

9 files changed

+87
-7
lines changed

9 files changed

+87
-7
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\User;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
class EmailAddressWasChanged
10+
{
11+
use Dispatchable;
12+
use SerializesModels;
13+
14+
/**
15+
* @var \App\User
16+
*/
17+
public $user;
18+
19+
public function __construct(User $user)
20+
{
21+
$this->user = $user;
22+
}
23+
}

app/Http/Controllers/Auth/RegisterController.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use App\Jobs\RegisterUser;
99
use App\Providers\RouteServiceProvider;
1010
use App\User;
11-
use Illuminate\Auth\Events\Registered;
1211
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
1312
use Illuminate\Foundation\Auth\RegistersUsers;
1413
use Illuminate\Support\Facades\Validator;
@@ -58,10 +57,6 @@ protected function validator(array $data): ValidatorContract
5857
*/
5958
protected function create(array $data): User
6059
{
61-
$user = $this->dispatchNow(RegisterUser::fromRequest(app(RegisterRequest::class)));
62-
63-
event(new Registered($user));
64-
65-
return $user;
60+
return $this->dispatchNow(RegisterUser::fromRequest(app(RegisterRequest::class)));
6661
}
6762
}

app/Jobs/DisapproveArticle.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
final class DisapproveArticle
88
{
9+
/**
10+
* @var \App\Models\Thread
11+
*/
912
private $article;
1013

1114
public function __construct(Article $article)

app/Jobs/GenerateSocialShareImage.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
final class GenerateSocialShareImage
99
{
10+
/**
11+
* @var \App\Models\Thread
12+
*/
1013
private $article;
1114

1215
const TEXT_X_POSITION = 50;

app/Jobs/UpdateProfile.php

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

33
namespace App\Jobs;
44

5+
use App\Events\EmailAddressWasChanged;
56
use App\Http\Requests\UpdateProfileRequest;
67
use App\User;
78
use Illuminate\Support\Arr;
@@ -36,8 +37,17 @@ public static function fromRequest(User $user, UpdateProfileRequest $request): s
3637

3738
public function handle(): User
3839
{
40+
$emailAddress = $this->user->emailAddress();
41+
3942
$this->user->update($this->attributes);
4043

44+
if ($emailAddress !== $this->user->emailAddress()) {
45+
$this->user->email_verified_at = null;
46+
$this->user->save();
47+
48+
event(new EmailAddressWasChanged($this->user));
49+
}
50+
4151
return $this->user;
4252
}
4353
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Listeners;
6+
7+
use App\Events\EmailAddressWasChanged;
8+
9+
final class SendEmailVerificationNotification
10+
{
11+
public function handle(EmailAddressWasChanged $event)
12+
{
13+
$event->user->sendEmailVerificationNotification();
14+
}
15+
}

resources/lang/en/settings.php

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

33
return [
44

5-
'updated' => 'Settings successfully saved!',
5+
'updated' => 'Settings successfully saved! If you changed your email address you\'ll receive an email address to re-confirm it.',
66
'deleted' => 'Account was successfully removed.',
77
'password.updated' => 'Password successfully changed!',
88

resources/views/users/settings/profile.blade.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
@formGroup('email')
2525
<label for="email">Email</label>
2626
<input type="email" name="email" id="email" value="{{ Auth::user()->emailAddress() }}" required />
27+
28+
@unless(Auth::user()->hasVerifiedEmail())
29+
<span class="text-gray-600 text-sm">
30+
This email address is not verified yet.
31+
<a href="{{ route('verification.notice') }}" class="text-green-primary">Resend verification email.</a>
32+
</span>
33+
@endunless
34+
2735
@error('email')
2836
@endFormGroup
2937

tests/Integration/Jobs/UpdateProfileTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Tests\Integration\Jobs;
44

5+
use App\Events\EmailAddressWasChanged;
56
use App\Jobs\UpdateProfile;
67
use Illuminate\Foundation\Testing\DatabaseMigrations;
8+
use Illuminate\Support\Facades\Event;
79
use Tests\TestCase;
810

911
class UpdateProfileTest extends TestCase
@@ -15,9 +17,30 @@ public function we_can_update_a_user_profile()
1517
{
1618
$user = $this->createUser();
1719

20+
Event::fake();
21+
1822
$updatedUser = $this->dispatch(new UpdateProfile($user, ['bio' => 'my bio', 'name' => 'John Doe Junior']));
1923

2024
$this->assertEquals('my bio', $updatedUser->bio());
2125
$this->assertEquals('John Doe Junior', $updatedUser->name());
26+
$this->assertDatabaseMissing('users', ['id' => $user->id, 'email_verified_at' => null]);
27+
28+
Event::assertNotDispatched(EmailAddressWasChanged::class);
29+
}
30+
31+
/** @test */
32+
public function changing_the_email_address_emits_an_event()
33+
{
34+
$user = $this->createUser();
35+
36+
Event::fake();
37+
38+
$this->dispatch(new UpdateProfile($user, ['email' => 'foo@example.com']));
39+
40+
$this->assertDatabaseHas('users', ['id' => $user->id, 'email_verified_at' => null]);
41+
42+
Event::assertDispatched(EmailAddressWasChanged::class, function (EmailAddressWasChanged $event) {
43+
return $event->user->email === 'foo@example.com';
44+
});
2245
}
2346
}

0 commit comments

Comments
 (0)