Skip to content

Commit

Permalink
Merge pull request #23 from nextapps-be/feature/allow_verification_wi…
Browse files Browse the repository at this point in the history
…thout_code_removal
  • Loading branch information
yinx authored May 6, 2022
2 parents 38eb158 + dc63dfb commit 51562dd
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 5 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

All notable changes to `laravel-verification-code` will be documented in this file

## 1.2.2 - 2022-05-06

- Added: parameter to verify method to prevent deleting of the code after verification
- Added: command to prune old codes from the database

## 1.2.1 - 2022-03-01

- Fix composer.json file to ensure package can still be used in Laravel 7/8

## 1.2.0 - 2022-03-01

- Add PHP 8.1 and Laravel 9 support ([#20](https://github.com/nextapps-be/laravel-verification-code/pull/20))

## 1.1.0 - 2021-02-15

- Add PHP 8.0 support ([#17](https://github.com/nextapps-be/laravel-verification-code/pull/17))

## 1.0.0 - 2021-01-15

- No changes
Expand Down
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 $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' => '[email protected]',
'created_at' => now()->subHours(5),
]);

$verificationCode = VerificationCode::create([
'code' => '123ABC',
'verifiable' => '[email protected]',
]);

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

$dbVerificationCodes = VerificationCode::all();

$this->assertCount(1, $dbVerificationCodes);
$this->assertEquals($verificationCode->id, $dbVerificationCodes->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' => '[email protected]',
]);

VerificationCodeFacade::verify('ABC123', '[email protected]', false);
$this->assertNotNull(VerificationCode::find($verificationCode->id));
}

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

0 comments on commit 51562dd

Please sign in to comment.