Generate a unique username (omniauth + devise)

I have an application with user authentication using devise + omniauth.

In my model, that username in my application is unique . I do not want to duplicate the username in my application.

Some facebook users do not have a specific username in their profile.

I want to create a unique username if the user has not defined the username on facebook.

For example, to generate a password, I have the following:

:password => Devise.friendly_token[0,20] 

How can I create a unique username for my application if the facebook user does not have a facebook username?

thanks

+4
source share
5 answers

You can take part in the email before the @ sign and add smth there, for example user_id, or just take the email. Or you can somehow combine the first and last name from the fb answer.

+2
source

You can create a nice readable username (for example, generated from the first part of the letter), and then make sure that it is unique by adding numbers until they are gone. eg,

 #in User def get_unique_login login_part = self.email.split("@").first new_login = login_part.dup num = 2 while(User.find_by_login(new_login).count > 0) new_login = "#{login_part}#{num}" end new_login end 

One of the problems is that someone can potentially summarize this login between you, and you save it. So, perhaps the best thing is to combine it into a before_create filter:

 #in User before_create :ensure_login_uniqueness def ensure_login_uniqueness if self.login.blank? || User.find_by_login(self.login).count > 0 login_part = self.email.split("@").first new_login = login_part.dup num = 2 while(User.find_by_login(new_login).count > 0) new_login = "#{login_part}#{num}" end self.login = new_login end end 
+10
source

This is how I created the Login with a combination of first and last name. Improvements in this code are welcome.

  before_create :ensure_login_uniqueness def ensure_login_uniqueness if self.login.blank? self.name = self.first_name + " " + self.last_name firstnamePart = self.first_name.downcase.strip.gsub(' ', '').gsub(/[^\w-]/, '') lastnamePart = self.last_name.downcase.strip.gsub(' ', '').gsub(/[^\w-]/, '') login_part = firstnamePart+lastnamePart new_login = login_part.dup num = 1 while(User.where(:login => new_login).count > 0) new_login = "#{login_part}#{num}" num += 1 end self.login = new_login end end 
+2
source

This did not work for me, but changed:

 while(User.find_by_login(new_login).count > 0) 

to

 while(User.where(login: new_login).count > 0) 
+1
source

Here are my methods that I use for Facebook

  def ensure_username_uniqueness self.username ||= self.email.split("@").first num = 2 until(User.find_by(username: self.username).nil?) self.username = "#{username_part}#{num}" num += 1 end end def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_create do |user| user.email = auth.info.email user.password = Devise.friendly_token[0,20] user.username = auth.info.name.downcase.gsub(" ", "") user.username = user.username[0..29] if user.username.length > 30 user.ensure_username_uniqueness end end 
+1
source

Source: https://habr.com/ru/post/1403856/


All Articles