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

Add configurable password length validation #64

Merged
merged 3 commits into from
Feb 27, 2024
Merged
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ authenticated do
end
```

### Password Length

The minimum acceptable password length for validation defaults to 12 characters but this can be configured in either `config/initializers/revise_auth.rb` or your environment configuration:
```ruby
ReviseAuth.configure do |config|
config.minimum_password_length = 42
end
```

## Contributing

If you have an issue you'd like to submit, please do so using the issue tracker in GitHub. In order for us to help you in the best way possible, please be as detailed as you can.
Expand Down
1 change: 1 addition & 0 deletions lib/revise_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ module ReviseAuth

config_accessor :sign_up_params, default: [:email, :password, :password_confirmation]
config_accessor :update_params, default: []
config_accessor :minimum_password_length, default: 12
end
2 changes: 1 addition & 1 deletion lib/revise_auth/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Model

validates :email, format: {with: URI::MailTo::EMAIL_REGEXP}, presence: true, uniqueness: true
validates :unconfirmed_email, format: {with: URI::MailTo::EMAIL_REGEXP}, allow_blank: true
validates_length_of :password, minimum: 12, allow_nil: true
validates_length_of :password, minimum: ReviseAuth.minimum_password_length, allow_nil: true
end

# Generates a confirmation token and send email to the user
Expand Down
12 changes: 12 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ class UserTest < ActiveSupport::TestCase
refute_empty user.errors.where(:password, :blank)
end

test "password meets minimum length" do
valid_user = User.new(email: "test@example.org")

valid_user.password = "a" * (ReviseAuth.minimum_password_length - 1)
valid_user.valid?
refute_empty valid_user.errors.where(:password, :too_short)

valid_user.password = "a" * ReviseAuth.minimum_password_length
valid_user.valid?
assert_empty valid_user.errors.where(:password, :too_short)
end

test "email is downcased" do
user = User.new(email: "TEST@example.org")
user.valid?
Expand Down
Loading