Skip to content
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

[1.x] Custom recovery codes #555

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 22 additions & 0 deletions src/RecoveryCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,35 @@

class RecoveryCode
{
/**
* The callback that should be used to generate a recovery code.
*
* @var (\Closure(mixed, string): string)|null
*/
public static $generateRecoveryCodeCallback;

/**
* Generate a new recovery code.
*
* @return string
*/
public static function generate()
{
if (static::$generateRecoveryCodeCallback) {
return call_user_func(static::$generateRecoveryCodeCallback);
}

return Str::random(10).'-'.Str::random(10);
}

/**
* Set a callback that should be used when generating a recovery code.
*
* @param \Closure(mixed, string): string $callback
* @return void
*/
public static function generateRecoveryCodeUsing($callback)
{
static::$generateRecoveryCodeCallback = $callback;
}
}
20 changes: 20 additions & 0 deletions tests/RecoveryCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Laravel\Fortify\Tests;

use Illuminate\Support\Str;
use Laravel\Fortify\RecoveryCode;

class RecoveryCodeTest extends OrchestraTestCase
{
public function test_recovery_codes_can_be_generated_with_a_custom_generator()
{
Str::createRandomStringsUsingSequence(['123', 'abc']);

RecoveryCode::generateRecoveryCodeUsing(function () {
return Str::random().'-'.Str::random();
});

$this->assertEquals('123-abc', RecoveryCode::generate());
}
}