1

gem devise + gem omniauthn (複数のプロバイダー) を使用した Rails アプリケーションがあります。このチュートリアルに従いました: http://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/ (アプリの例: https://github.com/klebershimabuku/fb_auth_demo )

ユーザーモデル:

  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

認証モデル:

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

認証コントローラー:

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

デバイスフォームに登録すると、名前や国などの必須の追加フィールドに入力できます。しかし、omniauth に登録すると、フォームがまったくないため、これらのフィールドに入力できず、登録後は NULL になります。私の質問: 必須の追加フィールドを含むフォームを omniauth 登録に追加するにはどうすればよいですか?

4

1 に答える 1

0

Omniauth は、他のログイン情報を使用してサイトにログインするために使用されます - Facebook や Twitter などから。

したがって、ユーザーが omniauth を使用してサイトにログオンしている場合、ユーザーがサイトへのログインに使用しているサイトから情報を収集できます。

例: ユーザーが Facebook からのログインを使用している場合、ユーザーから取得した都市やその他の情報は、ユーザーがログインに使用しているサイトから収集できます。したがって、(疑似コード)から取得できる追加情報:

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

編集:これが言ったので、このrailcast How to add additional fields to the registration.からの情報に従うことができます。

また、create device/omniauth をフォローする別の方法は、このページをフォローすることです(ただ言うだけです)。

于 2013-03-20T13:23:37.043 に答える