generate unique username (omniauth + devise)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 08:39:43

You can take a part of email before the @ sign and add there smth like user_id, or just take the email itself. Or you can combine somehow the first and last names from the fb response.

Max Williams

You can create a nice readable username (eg generated from the first part of the email) and then ensure it is unique by adding numbers until it is. 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}"
    num += 1
  end
  new_login
end

One problem here is that someone could potentially bag that login inbetween you getting it and you saving it. So, maybe it's best 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}"
      num += 1
    end
    self.login = new_login
  end
end

Here is how i created Login with combination of first name and last name field.. Improvements on this code is 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

It did not work for me,but change:

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

to

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

Here's 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!