简体   繁体   中英

The form with required fields in omniauth registration. (Ruby on Rails)

I have Rails application with gem devise + gem omniauthn (multiple providers). I followed to this tutorial: http://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/ (example of app: https://github.com/klebershimabuku/fb_auth_demo )

User model:

  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  attr_accessible  :name, :email, :country, :password, :password_confirmation, :remember_me
  has_many :authentications, :dependent => :delete_all

  def apply_omniauth(auth)
    self.email = auth['extra']['raw_info']['email']
    authentications.build(:provider => auth['provider'], :uid => auth['uid'], :token => auth['credentials']['token'])
  end

Authentications model:

attr_accessible :provider, :token, :uid, :user_id
belongs_to :user

Authentications Controller:

def create
    auth = request.env["omniauth.auth"]

    # Try to find authentication first
    authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid'])

    if authentication
      # Authentication found, sign the user in.
      flash[:notice] = "Signed in successfully."
      sign_in_and_redirect(:user, authentication.user)
    else
      # Authentication not found, thus a new user.
      user = User.new
      user.apply_omniauth(auth)
      if user.save(:validate => false)
        flash[:notice] = "Account created and signed in successfully."
        sign_in_and_redirect(:user, user)
      else
        flash[:error] = "Error while creating a user account. Please try again."
        redirect_to root_url
      end
    end
  end

When I register with devise form I can fill in some required extra fields like name and country. But when I register with omniauth I can't fill these fields, because it has not form at all, so they are NULL after registration. So my question: how can I add a form with required extra fields to omniauth registration?

Omniauth is used to login using other login information, to your site - like from facebook, twitter etc.

So, when a user is logging onto you site using omniauth, you can gather information from site the user is using to login to your site.

For example: If a user is using a login from Facebook, that city and other information you get from from the user can be gathered from the very site the user is using to login. So additional information you can get from (pseudocode):

 auth['extra']['raw_info']['state']

EDIT : Haing this said, you can follow information from this railcast How to add additional fields to the registration.

Also, another way to follow create device/omniauth is to follow this page (just saying)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM