Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ VerificationCode::verify($code, $email);
```
If the verification code is expired or does not match the user's email address, it will return `false`. If valid, it will return `true` and delete the code.

If you do not want the code to be deleted (in case the same code needs to be verified at different points in the login flow) you can pass a third parameter.

```php
use NextApps\VerificationCode\VerificationCode;

VerificationCode::verify($code, $email, $deleteAfterVerification);
```


### Verification codes table cleanup

Since it is possible for the verification codes table to fill up with unused codes, the following command will prune all codes older than the given hours.

```php
php artisan verification-code:prune --hours=24
```

## Config settings

### Length
Expand Down
18 changes: 18 additions & 0 deletions src/Console/PruneCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace NextApps\VerificationCode\Console;

use Illuminate\Console\Command;
use NextApps\VerificationCode\Models\VerificationCode;

class PruneCommand extends Command
{
protected $signature = 'verification-code:prune {--hours=24 : The number of hours to retain verification codes}';

protected $description = 'Prune old verification codes';

public function handle() : void
{
VerificationCode::where('created_at', '<=', now()->subHours($this->option('hours')))->delete();
}
}
7 changes: 5 additions & 2 deletions src/VerificationCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public function send(string $verifiable, string $channel = 'mail')
*
* @param string $code
* @param string $verifiable
* @param bool $cleanup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param bool $cleanup
* @param bool $deleteAfterVerification

*
* @return bool
*/
public function verify(string $code, string $verifiable)
public function verify(string $code, string $verifiable, bool $deleteAfterVerification = true)
{
if ($this->isTestVerifiable($verifiable)) {
return $this->isTestCode($code);
Expand All @@ -64,7 +65,9 @@ public function verify(string $code, string $verifiable)
return false;
}

VerificationCode::for($verifiable)->delete();
if ($deleteAfterVerification) {
VerificationCode::for($verifiable)->delete();
}

return true;
}
Expand Down
3 changes: 3 additions & 0 deletions src/VerificationCodeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NextApps\VerificationCode;

use Illuminate\Support\ServiceProvider;
use NextApps\VerificationCode\Console\PruneCommand;

class VerificationCodeServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -32,5 +33,7 @@ public function register()
});

$this->mergeConfigFrom(__DIR__ . '/../config/verification-code.php', 'verification-code');

$this->commands([PruneCommand::class]);
}
}
31 changes: 31 additions & 0 deletions tests/Commands/PruneCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace NextApps\VerificationCode\Tests\Feature;

use NextApps\VerificationCode\Models\VerificationCode;
use NextApps\VerificationCode\Tests\TestCase;

class PruneCommandTest extends TestCase
{
/** @test */
public function it_cleans_the_verification_code_table()
{
VerificationCode::create([
'code' => 'ABC123',
'verifiable' => 'taylor@laravel.com',
'created_at' => now()->subHours(5),
]);

$verificationCode = VerificationCode::create([
'code' => '123ABC',
'verifiable' => 'taylor@laravel.com',
]);

$this->artisan('verification-code:prune', ['--hours' => 3]);

$dbVerificationCode = VerificationCode::all();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$dbVerificationCode = VerificationCode::all();
$dbVerificationCodes = VerificationCode::all();


$this->assertCount(1, $dbVerificationCode);
$this->assertEquals($verificationCode->id, $dbVerificationCode->first()->id);
}
}
12 changes: 12 additions & 0 deletions tests/Feature/VerifyVerificationCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public function it_deletes_code_if_used_for_verification()
$this->assertNull(VerificationCode::find($verificationCode->id));
}

/** @test */
public function it_does_not_delete_code_if_cleanup_is_false()
{
$verificationCode = VerificationCode::create([
'code' => 'ABC123',
'verifiable' => 'taylor@laravel.com',
]);

VerificationCodeFacade::verify('ABC123', 'taylor@laravel.com', false);
$this->assertNotNull(VerificationCode::find($verificationCode->id));
}

/** @test */
public function it_returns_true_if_test_code_used_by_test_verifiable()
{
Expand Down