Skip to content

Commit

Permalink
Require at least 3 characters for password length
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk committed Oct 29, 2023
1 parent 601f1f8 commit 15b5e80
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Password::make('password')
As with the copy to clipboard action button, you can have this action rendered inline on the input as well by calling the `inlineSuffix()` method
on the input.

### Password Generation
### Password Generation Method

By default, the password generation is handled with Laravel's `Str::password()` helper method. This will generate a random, strong password that is 32
characters long for you. If you have a `maxLength()` set on the input, that length will be used instead for the character length.
Expand All @@ -208,6 +208,20 @@ Password::make('password')
}),
```

### Password Max Length

When using the default password generator (`Str::password()`), we will tell it to use the `maxLength()` that is set on the input. This means that
if you set a maximum length of 10 characters, the password generated by this action will be 10 characters long. By default, it is 32 characters long
if a max length is not set.

```php
Password::make('password')
->regeneratePassword()
->maxLength(10),
```

> **Note:** Due to how Laravel's `Str::password()` helper works, the password max length must be a minimum of 3 characters long.
Now when the button is clicked, `my-custom-password` will be filled into the input instead.

### Button Icon
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/CanRegeneratePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function generateNewSecret(mixed $state): string

$maxLength = $this->getMaxLength() ?? 32;

return Str::password($maxLength);
return Str::password(max(3, $maxLength));
}

public function getRegeneratePasswordAction(): Action
Expand Down
15 changes: 13 additions & 2 deletions tests/Concerns/CanRegeneratePasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,22 @@

it("respects the input's maxlength when generating a new password with the default generator", function () {
$input = Password::make('password')
->maxlength(2);
->maxlength(3);

expect($input->generateNewSecret(null))->toHaveLength(2);
expect($input->generateNewSecret(null))->toHaveLength(3);
});

test('a minimum of 3 characters is required when using the default password generator helper on the string class', function (int $minLength) {
$input = Password::make('password')
->maxLength($minLength);

expect($input->generateNewSecret(null))->toHaveLength(3);
})->with([
0,
1,
2,
]);

class CanRegenerateWithButton extends Livewire
{
public function form(Form $form): Form
Expand Down

0 comments on commit 15b5e80

Please sign in to comment.