Skip to content

Verify OTP without marking it as used. #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ use Ichtrojan\Otp\Otp;
}
```

### Check The validity of an OTP

To verify the validity of an OTP without marking it as used, you can use the `isValid` method:

```php
<?php
use Ichtrojan\Otp\Otp;

(new Otp)->isValid(string $identifier, string $token);
```

This will return a boolean value of the validity of the OTP.

### Delete expired tokens
You can delete expired tokens by running the following artisan command:
```bash
Expand Down
18 changes: 18 additions & 0 deletions src/Otp.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ public function generate(string $identifier, string $type, int $length = 4, int
];
}

/**
* @param string $identifier
* @param string $token
* @return bool
*/
public function isValid(string $identifier, string $token): bool
{
$otp = Model::where('identifier', $identifier)->where('token', $token)->first();

if ($otp instanceof Model) {
$validity = $otp->created_at->addMinutes($otp->validity);

return Carbon::now()->lt($validity) && $otp->valid;
}

return false;
}

/**
* @param string $identifier
* @param string $token
Expand Down