Skip to content

Commit 51562dd

Browse files
authored
Merge pull request #23 from nextapps-be/feature/allow_verification_without_code_removal
2 parents 38eb158 + dc63dfb commit 51562dd

File tree

7 files changed

+94
-5
lines changed

7 files changed

+94
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

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

5+
## 1.2.2 - 2022-05-06
6+
7+
- Added: parameter to verify method to prevent deleting of the code after verification
8+
- Added: command to prune old codes from the database
9+
510
## 1.2.1 - 2022-03-01
611

712
- Fix composer.json file to ensure package can still be used in Laravel 7/8
8-
13+
914
## 1.2.0 - 2022-03-01
1015

1116
- Add PHP 8.1 and Laravel 9 support ([#20](https://github.com/nextapps-be/laravel-verification-code/pull/20))
12-
17+
1318
## 1.1.0 - 2021-02-15
1419

1520
- Add PHP 8.0 support ([#17](https://github.com/nextapps-be/laravel-verification-code/pull/17))
16-
21+
1722
## 1.0.0 - 2021-01-15
1823

1924
- No changes

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ VerificationCode::verify($code, $email);
4949
```
5050
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.
5151

52+
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.
53+
54+
```php
55+
use NextApps\VerificationCode\VerificationCode;
56+
57+
VerificationCode::verify($code, $email, $deleteAfterVerification);
58+
```
59+
60+
61+
### Verification codes table cleanup
62+
63+
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.
64+
65+
```php
66+
php artisan verification-code:prune --hours=24
67+
```
68+
5269
## Config settings
5370

5471
### Length

src/Console/PruneCommand.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace NextApps\VerificationCode\Console;
4+
5+
use Illuminate\Console\Command;
6+
use NextApps\VerificationCode\Models\VerificationCode;
7+
8+
class PruneCommand extends Command
9+
{
10+
protected $signature = 'verification-code:prune {--hours=24 : The number of hours to retain verification codes}';
11+
12+
protected $description = 'Prune old verification codes';
13+
14+
public function handle() : void
15+
{
16+
VerificationCode::where('created_at', '<=', now()->subHours($this->option('hours')))->delete();
17+
}
18+
}

src/VerificationCodeManager.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ public function send(string $verifiable, string $channel = 'mail')
4343
*
4444
* @param string $code
4545
* @param string $verifiable
46+
* @param bool $deleteAfterVerification
4647
*
4748
* @return bool
4849
*/
49-
public function verify(string $code, string $verifiable)
50+
public function verify(string $code, string $verifiable, bool $deleteAfterVerification = true)
5051
{
5152
if ($this->isTestVerifiable($verifiable)) {
5253
return $this->isTestCode($code);
@@ -64,7 +65,9 @@ public function verify(string $code, string $verifiable)
6465
return false;
6566
}
6667

67-
VerificationCode::for($verifiable)->delete();
68+
if ($deleteAfterVerification) {
69+
VerificationCode::for($verifiable)->delete();
70+
}
6871

6972
return true;
7073
}

src/VerificationCodeServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace NextApps\VerificationCode;
44

55
use Illuminate\Support\ServiceProvider;
6+
use NextApps\VerificationCode\Console\PruneCommand;
67

78
class VerificationCodeServiceProvider extends ServiceProvider
89
{
@@ -32,5 +33,7 @@ public function register()
3233
});
3334

3435
$this->mergeConfigFrom(__DIR__ . '/../config/verification-code.php', 'verification-code');
36+
37+
$this->commands([PruneCommand::class]);
3538
}
3639
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace NextApps\VerificationCode\Tests\Feature;
4+
5+
use NextApps\VerificationCode\Models\VerificationCode;
6+
use NextApps\VerificationCode\Tests\TestCase;
7+
8+
class PruneCommandTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_cleans_the_verification_code_table()
12+
{
13+
VerificationCode::create([
14+
'code' => 'ABC123',
15+
'verifiable' => 'taylor@laravel.com',
16+
'created_at' => now()->subHours(5),
17+
]);
18+
19+
$verificationCode = VerificationCode::create([
20+
'code' => '123ABC',
21+
'verifiable' => 'taylor@laravel.com',
22+
]);
23+
24+
$this->artisan('verification-code:prune', ['--hours' => 3]);
25+
26+
$dbVerificationCodes = VerificationCode::all();
27+
28+
$this->assertCount(1, $dbVerificationCodes);
29+
$this->assertEquals($verificationCode->id, $dbVerificationCodes->first()->id);
30+
}
31+
}

tests/Feature/VerifyVerificationCodeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ public function it_deletes_code_if_used_for_verification()
5757
$this->assertNull(VerificationCode::find($verificationCode->id));
5858
}
5959

60+
/** @test */
61+
public function it_does_not_delete_code_if_cleanup_is_false()
62+
{
63+
$verificationCode = VerificationCode::create([
64+
'code' => 'ABC123',
65+
'verifiable' => 'taylor@laravel.com',
66+
]);
67+
68+
VerificationCodeFacade::verify('ABC123', 'taylor@laravel.com', false);
69+
$this->assertNotNull(VerificationCode::find($verificationCode->id));
70+
}
71+
6072
/** @test */
6173
public function it_returns_true_if_test_code_used_by_test_verifiable()
6274
{

0 commit comments

Comments
 (0)