Skip to content

Commit

Permalink
feat: update verification code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ibnnajjaar committed Sep 11, 2024
1 parent 177b1dd commit 4f039bc
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
7 changes: 6 additions & 1 deletion config/mobile-verification.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,10 @@
'register' => \Javaabu\MobileVerification\Notifications\RegisterVerificationTokenNotification::class,
'verification_code' => \Javaabu\MobileVerification\Notifications\MobileNumberVerificationToken::class,
'update' => \Javaabu\MobileVerification\Notifications\MobileNumberVerificationToken::class,
]
],

/*
* Token generator class
*/
'verification_code_generator' => \Javaabu\MobileVerification\Support\VerificationCodeGenerator::class,
];
3 changes: 2 additions & 1 deletion lang/en/strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
'exists' => 'The :attribute has already been taken.',
'doesnt-exist' => 'The :attribute does not exist.',
'locked' => 'The number is locked due to too many attempts. You can try again in :time.',
'recently_sent' => 'The code was recently sent to this number. You can try again in :time.'
'recently_sent' => 'The code was recently sent to this number. You can try again in :time.',
'soft_deleted' => 'The user associated with the :attribute is soft deleted.',
],
'country_code' => [
'invalid' => 'The country code is invalid.'
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/IsVerificationCodeGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace Javaabu\MobileVerification\Contracts;


interface IsVerificationCodeGenerator
{
public function handle(): string;
}
4 changes: 4 additions & 0 deletions src/Rules/IsValidMobileNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
->hasPhoneNumber($this->getCountryCode(), $value, $this->user_type)
->first();

if ($mobile_number && $mobile_number->user_id && ! $mobile_number->user) {
$fail(trans('mobile-verification::mobile-verification::strings.validation.number.soft_deleted', ['attribute' => $attribute]));
}

if ($this->should_be_registered_number) {
if (empty($mobile_number) || empty($mobile_number->user_id)) {
$fail(trans('mobile-verification::strings.validation.number.doesnt-exist', ['attribute' => $attribute]));
Expand Down
3 changes: 2 additions & 1 deletion src/Support/VerificationCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Carbon\Carbon;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Javaabu\MobileVerification\Contracts\IsVerificationCodeGenerator;

class VerificationCodeGenerator
class VerificationCodeGenerator implements IsVerificationCodeGenerator
{
public function handle(): string
{
Expand Down
7 changes: 6 additions & 1 deletion src/Support/VerificationCodeUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
use Carbon\Carbon;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Javaabu\MobileVerification\Contracts\IsVerificationCodeGenerator;

class VerificationCodeUpdater
{

public IsVerificationCodeGenerator $verificationCodeGenerator;
public function __construct(
public VerificationCodeGenerator $verificationCodeGenerator
)
{
$verification_code_generator_class = config('mobile-verification.verification_code_generator');
$this->verificationCodeGenerator = app($verification_code_generator_class);
}

public function handle(Model $mobile_number): string
{
$verification_code = $this->verificationCodeGenerator->handle();
Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/VerificationCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,40 @@ public function test_too_many_invalid_attempts_will_lock_the_mobile_number()
->assertJsonValidationErrors(['number']);
}

public function test_verification_code_request_to_soft_deleted_user_returns_correct_validation_error()
{
$user = User::factory()->create();
MobileNumber::unguard();

$mobile_number = MobileNumber::create([
'number' => '7528222',
'country_code' => '960',
'user_type' => 'user',
'user_id' => $user->id,
]);

$user->delete();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'deleted_at' => now(),
]);

$this->mock(VerificationCodeGenerator::class, function ($mock) use ($mobile_number) {
$mock->shouldReceive('handle')
->andReturn('123456');
});

$response = $this->postJson('/api/mobile-verifications/verification-code', [
'user_type' => 'user',
'number' => '7528222',
]);

$response->assertJsonValidationErrors(['number']);

$mobile_number->refresh();
$this->assertNull($mobile_number->verification_code);
}

public function getUserWithMobileNumber(string $number = '7528222'): User
{
$user = User::factory()->create();
Expand Down

0 comments on commit 4f039bc

Please sign in to comment.