Skip to content

Commit

Permalink
Add support only confirm token
Browse files Browse the repository at this point in the history
  • Loading branch information
foladgar committed Jun 21, 2024
1 parent 7b4f970 commit fd96925
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
6 changes: 3 additions & 3 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
'user_providers' => [
'users' => [
'table' => 'users',
'model' => \App\Models\User::class,
'repository' => \Fouladgar\OTP\NotifiableRepository::class,
'model' => App\Models\User::class,
'repository' => Fouladgar\OTP\NotifiableRepository::class,
],

// 'admins' => [
Expand Down Expand Up @@ -126,5 +126,5 @@
| If you use default channel you must set "sms_client". Otherwise you don't need that.
|
*/
'channel' => \Fouladgar\OTP\Notifications\Channels\OTPSMSChannel::class,
'channel' => Fouladgar\OTP\Notifications\Channels\OTPSMSChannel::class,
];
13 changes: 12 additions & 1 deletion src/OTPBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class OTPBroker

private ?string $token = null;

private bool $onlyConfirm = false;

private NotifiableRepositoryInterface $userRepository;

public function __construct(
Expand Down Expand Up @@ -59,13 +61,22 @@ public function validate(string $mobile, string $token, bool $create = true): OT

throw_unless($this->tokenExists($notifiable, $token), InvalidOTPTokenException::class);

$notifiable = $this->find($mobile, $create);
if(!$this->onlyConfirm){
$notifiable = $this->find($mobile, $create);
}

$this->revoke($notifiable);

return $notifiable;
}

public function onlyConfirmToken(bool $confirm = true): static
{
$this->onlyConfirm = $confirm;

return $this;
}

public function getToken(): ?string
{
return $this->token;
Expand Down
29 changes: 25 additions & 4 deletions tests/OTPBrokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class OTPBrokerTest extends TestCase
{
protected const MOBILE = '09389599530';

public function setUp(): void
{
parent::setUp();

config()->set('otp.user_providers.users.model', OTPNotifiableUser::class);
}

/**
* @test
*/
Expand Down Expand Up @@ -197,9 +204,23 @@ public function it_can_send_by_using_provider(): void
);
}

public function setUp(): void
/**
* @test
*/
public function it_can_only_confirm_token_and_does_not_create_user(): void
{
parent::setUp();
config()->set('otp.user_providers.users.model', OTPNotifiableUser::class);
$otp = OTP();

$otp->send(self::MOBILE, false);

$user = $otp->onlyConfirmToken()
->validate(
self::MOBILE,
Cache::get(self::MOBILE)['token']
);

$this->assertInstanceOf(OTPNotifiable::class, $user);

$this->assertEquals(0, OTPNotifiableUser::count());
}
}
}

0 comments on commit fd96925

Please sign in to comment.