-
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #741 from pinkary-project/feat--actions-for-verifi…
…ed-users Feat: actions for Email verified users
- Loading branch information
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Livewire\Concerns; | ||
|
||
trait NeedsVerifiedEmail | ||
{ | ||
/** | ||
* Determine if the user does not have a verified email address. | ||
*/ | ||
public function doesNotHaveVerifiedEmail(): bool | ||
{ | ||
if (auth()->user()?->hasVerifiedEmail()) { | ||
return false; | ||
} | ||
|
||
session()->flash('flash-message', 'You must verify your email address before you can continue.'); | ||
|
||
$this->redirectRoute('verification.notice', navigate: true); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Livewire\Concerns\NeedsVerifiedEmail; | ||
use App\Models\User; | ||
use Livewire\Component; | ||
use Livewire\Livewire; | ||
|
||
it('can check if the user does not have a verified email address', function () { | ||
$user = User::factory()->create([ | ||
'email_verified_at' => null, | ||
]); | ||
|
||
$component = Livewire::actingAs($user)->test(myComponentNeedsVerifiedEmail()::class); | ||
$component->call('someMethod'); | ||
$component->assertRedirect(route('verification.notice')); | ||
$component->assertSet('isConfirmed', false); | ||
|
||
expect(session('flash-message'))->toBe('You must verify your email address before you can continue.'); | ||
}); | ||
|
||
it('can check if the user has a verified email address', function () { | ||
$user = User::factory()->create(); | ||
|
||
$component = Livewire::actingAs($user)->test(myComponentNeedsVerifiedEmail()::class); | ||
$component->call('someMethod'); | ||
$component->assertSet('isConfirmed', true); | ||
|
||
expect(session('flash-message'))->toBeNull(); | ||
}); | ||
|
||
function myComponentNeedsVerifiedEmail(): Component | ||
{ | ||
return new class() extends Component | ||
{ | ||
use NeedsVerifiedEmail; | ||
|
||
public bool $isConfirmed = false; | ||
|
||
public function someMethod() | ||
{ | ||
if ($this->doesNotHaveVerifiedEmail()) { | ||
return; | ||
} | ||
|
||
$this->isConfirmed = true; | ||
} | ||
|
||
public function render() | ||
{ | ||
return <<<'HTML' | ||
<div> | ||
<button wire:click="someMethod">Click me</button> | ||
</div> | ||
HTML; | ||
} | ||
}; | ||
} |