Skip to content

Commit 084b29e

Browse files
committed
make sure UserVerified is only emitted once #9
1 parent 9f3d5f3 commit 084b29e

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

src/EmailVerification.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,11 @@ public function verify(array $credentials, Closure $callback)
150150
return $user;
151151
}
152152

153-
$callback($user);
153+
if($callback($user)) {
154154

155-
$this->events->dispatch(new UserVerified($user));
155+
$this->events->dispatch(new UserVerified($user));
156+
157+
}
156158

157159
return static::VERIFIED;
158160
}

src/Traits/VerifiesEmail.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function verify(Request $request, EmailVerification $emailVerification)
3939
$request->only(
4040
'email', 'expiration', 'token'
4141
), function ($user) {
42-
$this->verifiedEmail($user);
42+
return $this->verifiedEmail($user);
4343
}
4444
);
4545

@@ -90,14 +90,20 @@ public function resendVerificationEmail(Request $request)
9090
* Store the user's verification
9191
*
9292
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
93-
* @return void
93+
* @return boolean
9494
*/
9595
protected function verifiedEmail($user)
9696
{
97-
$user->forceFill([
98-
'verified' => true
99-
])->save();
10097
$this->guard()->login($user);
98+
99+
if(!(boolean)$user->verified) {
100+
$user->forceFill([
101+
'verified' => true
102+
])->save();
103+
return true;
104+
}
105+
106+
return false;
101107
}
102108

103109
/**

tests/Feature/RegistrationTest.php

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

1010

1111

12+
use Illuminate\Support\Facades\Event;
1213
use Illuminate\Support\Facades\Notification;
14+
use Lunaweb\EmailVerification\Events\UserVerified;
1315
use Lunaweb\EmailVerification\Notifications\EmailVerification;
1416

1517
class RegistrationTest extends TestCase
@@ -26,6 +28,7 @@ public function setUp()
2628
Notification::fake();
2729

2830

31+
2932
}
3033

3134

@@ -66,4 +69,43 @@ public function testRegistration() {
6669

6770

6871

72+
public function testEmitsUserVerifedEventOnce() {
73+
74+
75+
Event::fake();
76+
77+
$user = User::create(['email' => '[email protected]', 'verified' => false]);
78+
79+
app(\Lunaweb\EmailVerification\EmailVerification::class)->sendVerifyLink($user);
80+
81+
$notification = Notification::sent($user, EmailVerification::class)->first();
82+
$activationUrl = $notification->toMail($user)->actionUrl;
83+
84+
// Open activation URL first time
85+
86+
$this->get($activationUrl);
87+
88+
Event::assertDispatched(UserVerified::class, function ($e) use ($user) {
89+
return $e->user->is($user);
90+
});
91+
92+
$this->assertTrue((boolean)$user->fresh()->verified);
93+
94+
95+
96+
// Open activation URL second time
97+
98+
$this->get($activationUrl);
99+
100+
Event::assertDispatchedTimes(UserVerified::class, 1);
101+
102+
$this->assertTrue((boolean)$user->fresh()->verified);
103+
104+
105+
106+
107+
}
108+
109+
110+
69111
}

tests/Feature/ResendVerificationMailTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testResend() {
5757

5858
$user->refresh();
5959
$this->assertTrue((boolean)$user->verified);
60-
$this->assertTrue($user->email === '[email protected]');
60+
$this->assertEquals('[email protected]', $user->email);
6161

6262

6363
}

0 commit comments

Comments
 (0)