I followed Devise's instructions for using usernames in addition to emails for logins. Works great except people can signup with duplicate usernames. If someone tries to signup with a duplicate email address, they are presented with this error message on the signup page:
1 error prohibited this user from being saved:
Email has already been taken
I want something very similar for usernames. How do I implement this?
You should use uniqueness validator. Add following into user.rb:
validates_uniqueness_of :username
more about this validator
Note that you probably want to make usernames unique, but also case-insensitive, so you don't end up with both username and Username:
class User < ApplicationRecord
validates :username, presence: true, uniqueness: { case_sensitive: false }
# Rest of user model
end
From the Devise wiki.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With