使用Omniauth-Facebook进行额外的用户属性验证失败

时间:2017-10-26 19:04:51

标签: ruby-on-rails ruby facebook devise omniauth-facebook

大家好,感谢您的专业知识和帮助。

我对Ruby on Rails(仅在3个月前开始)并不熟练,并且正在构建一个相当复杂的应用程序。关于Omniauth-Facebook,我遇到了障碍。我有Devise,omniauth-facebook,omniauth_callbacks控制器,Devise registrations_controller,Devise users_controller以及具有多个字段验证的用户模型(请参阅下面的所有代码)。在设置这一切时,我或多或少地关注了这个Youtube视频:https://www.youtube.com/watch?v=VeUX3pWn28w以及我尝试进行故障排除时的许多其他分散指南。有些已经过时,我甚至不确定我的代码的哪些部分可能已过时。

从我的注册表单开始工作:用户可以填写我的自定义注册表单并正确添加到数据库中,所有Devise操作都正常运行并记录所有自定义表格属性。用户可以点击"注册Facebook"按钮并被定向到Facebook进行登录/注册,其中返回提供者和UID,以及(似乎)名字,电子邮件和密码。我的架构中有State和Zip默认值,因此不会引发验证异常。

似乎失败的是:当Facebook发送回信息时,它似乎会在电子邮件返回时中断("用户存在")。即使我已经包含了" auth.info.last_name,auth.info.image等等,终端错误也从未显示从Facebook返回的那些额外字段,但验证结果不是'抛出异常,包括电子邮件。在我的数据库中,我确保没有该电子邮件的重复。由于我的数据库和验证,似乎我所需的姓氏,性别,DOB,地址和城市也没有填写,这是可以理解的(除了last_name)因为我无法从Facebook接收。

这就是我想要的:正如我上面提到的,Facebook似乎没有返回" last_name","地址"或"图像"。我还需要包括出生日期,性别和他们的城市。似乎我无法从omniauth-facebook那样做,因为Facebook的Auth Hash并不包括这些领域。我如何允许用户通过Facebook注册这些提供的字段,通过Facebook登录,然后被引导到一个页面,在那里他们可以输入我的数据库所需要的其他信息?因此,在通过Facebook注册后,如何将用户发送到另一个页面(没有抛出那些验证异常?)然后输入其他信息(姓氏,出生日期,地址,地址2 [if需要],性别,城市/州/邮编,或/否则缺少任何其他字段)?为什么可能" last_name"失败了,但" first_name"很好吗?

下面是你们可能需要的所有内容的图片,我认为可能是必需的。同样,我理解Ruby和Rails的基础知识,并认为自己是中级的,但需要帮助理解为什么/如何解决我正在经历的这个问题的答案。我遗憾地缺乏有关高级路由和控制器如何工作的知识,所以如果你有任何资源可以帮助我提高我对该主题的认识,我也会很感激。

用户表(schema.rb):

  create_table "users", force: :cascade do |t|
    t.string "first_name", null: false
    t.string "last_name", null: false
    t.boolean "admin", default: false
    t.boolean "subscribed", default: true
    t.string "zip", default: "44107", null: false
    t.string "phone_number"
    t.string "address", null: false
    t.string "city_name", default: "Lakewood", null: false
    t.string "state", default: "Ohio", null: false
    t.date "dob", null: false
    t.integer "city_id"
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.string "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string "unconfirmed_email"
    t.integer "failed_attempts", default: 0, null: false
    t.string "unlock_token"
    t.datetime "locked_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "address2"
    t.boolean "gender"
    t.string "provider"
    t.string "uid"
    t.string "name"
    t.text "image"
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
  end 

用户模型(user.rb):

class User < ApplicationRecord
      # Below - Devise Modules. We have not used = :timeoutable 
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, 
             :validatable, :confirmable, 
             :omniauthable, omniauth_providers: [:facebook]

      # Below - Additional validations of DB field entries presence at Model-level.
      validates :first_name, presence: true, length: { maximum: 30 }
      validates :last_name, presence: true, length: { maximum: 30 }
      validates :dob, presence: { message: "(Date of Birth) must be entered" } 
      validates :zip, presence: true, length: { maximum: 11 }
      validates :city_name, presence: true   
      validates :state, presence: true, length: { maximum: 15 }
      validates :address, presence: true
      validates :gender, presence: { message: "must be selected" } 

       # Below - Associates Users into a One to Many relationship with Cities.
      belongs_to :city


      def self.new_with_session(params, session)
        super.tap do |user|
          if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
            user.email = data["email"] if user.email.blank?
          end
        end
      end


      def self.from_omniauth(auth)
        where(provider: auth.provider, uid: auth.uid).first_or_create! do |user|
          user.provider       = auth.provider
          user.uid            = auth.uid
          user.email          = auth.info.email
          user.first_name     = auth.info.first_name
          user.password       = Devise.friendly_token[0,20] 
          user.last_name      = auth.info.last_name
          user.image          = auth.info.image
          user.address        = auth.info.location
          user.skip_confirmation!

          user.save

      end


      end

      # Below - Turns emails into downcase when saved from the controller into DB.      
      before_save { self.email = email.downcase }

    end

会话控制器(controllers / users / sessions_controller.rb):

class Users::SessionsController < Devise::SessionsController
  # before_action :configure_sign_in_params, only: [:create]
  class SessionsController < ApplicationController
    def create
      @user = User.find_or_create_from_auth_hash(auth_hash)
      self.current_user = @user
      redirect_to '/'
    end

    protected

    def auth_hash
      request.env['omniauth.auth']
    end
  end

注册控制器(controllers / users / registrations_controller.rb):

class Users::RegistrationsController < Devise::RegistrationsController
   before_action :configure_sign_up_params, only: [:create]
  # before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
   def new
     @user = User.new
   end

  # POST /resource
  def create
    super
  end

  # GET /resource/edit
   def edit
     super
   end

  # PUT /resource
   def update
     super
   end

  protected

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :gender, :dob, :admin, :phone_number, :address, :address2, :city_name, :state, :zip, :subscribed, :city_id, :name, :image, :uid, :provider])
  end

end 

用户控制器(controllers / users_controller.rb):

class UsersController < ApplicationController

    # Main Users Controller

      # Index Action for all Users
      def index
        @users = User.all
      end

      # Show Action for individual user ID
      def show 
        @user = User.find(params[:id])
      end 
    end

Omniauth回调控制器(controllers / user / omniauth_callbacks_controller.rb):

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
# Main MOdel for Facebook OmniAuth User Login/Signup

  def facebook
    #raise request.env["omniauth.auth"].to_yaml
    @user = User.from_omniauth(request.env["omniauth.auth"])

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
    # 
  end

  def failure
    redirect_to root_path
  end

end

应用程序控制器(controllers / application_controller.rb):

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

# Below, permits strong params from signup process, sign_in, and Account_update
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:login, keys: [:email, :password])
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :address, :address2, :dob, :city_name, :state, :zip, :gender, :phone_number, :subscribed, :email, :password, :password_confirmation, :city_id, :name, :image, :uid, :provider]) 
    devise_parameter_sanitizer.permit(:account_update, keys: [:email, :address, :address2, :city_name, :state, :zip, :phone_number, :subscribed, :password, :password_confirmation, :current_password])
  end

end

设计初始化程序(config / initializers / devise.rb):

 # ==> OmniAuth
  # Add a new OmniAuth provider. Check the wiki for more information on setting
  # up on your models and hooks.
  config.omniauth :facebook, 'BLANK', 'BLANK', callback_url: 'MYWEBADRESS/users/auth/facebook/callback'

路线(routes.rb):

Rails.application.routes.draw do
# Main Routes file for all route, URL names and Action Methods. 

  # Below - Sets up custom login and log out path names for routes for user model.
  devise_for :users, path_names: { sign_in: "login", sign_out: "logout" }, controllers: {
                                    sessions: 'users/sessions', 
                                    :omniauth_callbacks => "users/omniauth_callbacks"   }

  # Not logged in visitors will be greeted by Index page
  root 'visitors#index'

  get 'cities/index'

  # Callback Route after OmniAuth redirects back to Ossemble for User Signup
  get '/auth/facebook/callback', to: 'sessions#create'

  # Below - Sets up routes for admin model.
  devise_for :admins

  # Below - Creates all routes for City model.
  resources :cities, except: [:destroy]

  # Below - Creates show route for Users & Model
  resources :users, only: [:show, :index]

end

最后我的注册表单(views / devise / registrations / new.html.erb):

<%= form_for(resource, as: resource_name, url: registration_path(resource_name),
      :html => {class: "form", role: "form"}) do |f| %>
    <div class="container alert"> <!-- Begin - Error Message(s) Wrapper for User Signup -->
      <%= devise_error_messages! %>
    </div>                        <!-- End - Error Message(s) Wrapper for User Signup -->
    <div class="container"> <!-- Begin -- Signup Form -->
      <div class="form-row"> <!-- Begin - First, Last Name, Gender, & Date of Birth Form Row -->
        <div class="form-group col-md-4">
          <div class="control-label">
            <%= f.label :first_name, "First Name" %> 
          </div>
            <%= f.text_field :first_name, autofocus: true, class: "form-control", placeholder: "Enter First Name" %>
        </div>
        <div class="form-group col-md-4">
          <div class="control-label">
            <%= f.label :last_name, "Last Name" %> 
          </div>
            <%= f.text_field :last_name, class: "form-control", placeholder: "Enter Last Name" %>
        </div>
       <div class="form-group col-md-2">
          <div class="control-label">
            <label class="control-label"> 
            Gender
            </label>
          </div>   
            <div class="gender_form form-control center" >
              <%= f.label :gender, "Male", id: "male_text", class: "gender_text" %>
              <%= f.radio_button :gender, true,  class: "gender_box"%>
              <%= f.label :gender, "Female", id: "female_text", class: "gender_text" %>
              <%= f.radio_button :gender, false, class: "gender_box" %>
            </div>
        </div>
        <div class="form-group col-md-2">
          <div class="control-label">
            <%= f.label :dob, "Birth Date" %> 
          </div>
            <%= f.date_field :dob, class: "form-control" %>
        </div>
      </div> 
      <br> <!-- End - of Name, Gender & DOB form row -->

    <!-- Begin - Email & Password Form Row -->
      <div class="form-row">
        <div class="form-group col-md-4">
          <div class="control-label">
            <%= f.label :email, "Email Address" %>
          </div>
            <%= f.email_field :email, type:"text", class: "form-control", placeholder: "Enter Email Address: youremail@example.com" %>
        </div>  
        <div class="form-group col-md-4">
          <div class="control-label">
            <%= f.label :password, "Password" %>
            <!-- Begin - If statement checks for password minimum character length  -->
            <small>
              (Minimum: <%= @minimum_password_length %> characters) 
            </small>
          </div>
            <%= f.password_field :password, autocomplete: "off", id:"inputPassword", class: "form-control", placeholder: "Create a Password" %>
          <!-- End - password character length if statement -->
        </div>
        <div class="form-group col-md-4">
          <div class="control-label">
            <%= f.label :password_confirmation, "Confirm Password" %> 
          </div>  
            <%= f.password_field :password_confirmation, autocomplete: "off", id:"inputPasswordConfirm", class: "form-control", placeholder: "Re-enter Password" %>
        </div>
      </div> 
      <br> <!-- End - Email & Password Form Row -->

    <!-- Begin -  Address & Phone Number Form Row--> 
      <div class="form-row">
        <div class="form-group col-md-4">
          <div class="control-label">
            <%= f.label :address, "Primary Address" %> 
            </div>
            <%= f.text_field :address, autocomplete: "on", class: "form-control", placeholder: "Enter Address: 1234 Main Street" %>
        </div>
        <div class="form-group col-md-4">
          <div class="control-label">
            <%= f.label :address2, "Secondary Address" %> <small> (optional) </small>
          </div>
            <%= f.text_field :address2, autocomplete: "on", class: "form-control", placeholder: "Apartment, Suite #" %>
        </div>
        <!--  Phonne number currently hideen on form due to "style-" and "disabled :true" -->
        <div class="form-group col-md-3" style="display: none">
          <div class="control-label">
            <%= f.label :phone_number, "Phone Number" %> 
          </div>
            <%= f.phone_field :phone_number, disabled: true, class: "form-control", placeholder: "Enter Phone #: 555-555-5555" %>
        </div>
      </div> <br> <!-- End - Address & Phone Number Row -->

  <!--  Beginning - Location Form Row  -->
      <div class="form-row">
        <div class="form-group col-md-4">
          <div class="control-label">
            <%= f.label :city_name, "City" %> 
              <small>
                (Currently Lakewood, OH Supported)
              </small>
          </div> 
<!-- Below, we list all cities to be selected and we set the user's "city_id" field to the corresponding City table's City ID, completing the User's Association to City  -->
            <%= f.collection_select :city_id, City.all, :id, :name, {prompt: "Choose a City"}, class: "select_form form-control" %>
        </div>
        <div class="form-group col-md-2 ">
          <div class="control-label">
            <%= f.label :state, "State" %> 
              <small>
                (Currently Ohio)
              </small>
          </div>
            <%= f.collection_select :state, City.all, :state, :state, {prompt: "Choose a State"}, class: "select_form form-control" %>
        </div>
        <div class="form-group col-md-2">
          <div class="control-label">
            <%= f.label :zip, "Zip Code" %> <small> (5-Digit Zip)</small>
          </div>
            <%= f.collection_select :zip, City.all, :zip, :zip, {prompt: "Choose Zip-Code"}, maxlength: "5", class: "select_form form-control" %>
        </div>
      </div> <!--  End - Location Form Row -->

    <!-- Begin - Subscribe Option -->
      <div class="form-row">
        <div class="form-group col-md-12 pull-left">
          <div class="form-check">
            <div class="control-label">
              <%= f.label :subscribed, "Subscribe?" %> 
                <small>
                  <em>
                  Stay up to date with Ossemble!
                  </em>
                </small> 
              <br>
              <div class="form-row pull-left">  
                <div class="col-md-12">
                  <div>
                    <%= f.check_box :subscribed, class: "check_box"  %>
                    <%= f.label "Yes!", class: "check_box_label"  %>
                  </div>
                </div>
              </div>
            </div> 
          </div> 
        </div>
      </div> <!-- End - Subscribe Option -->
      <div class="form-row"> <!-- Begin - Create Account Button Row (FB) -->  
        <div class="form-group col-md-2">
          <div class="actions">
            <%= f.submit "Create Account", class: "form_btn btn btn-success btn-lg btn-xlarge" %>
          </div>
        </div> 
        <div class="form-group col-md-4"> <!-- Begin - Signup With Facebook Button -->
          <%- if devise_mapping.omniauthable? %> <!-- Begin - OmniAuth If Statement -->
              <!-- Below - OmniAuth & FB Signup Button -->
              **<%= link_to '<i class="fa fa-facebook fa-lg fa-fw fb_log_sign_icon" aria-hidden="true"></i> Facebook Signup'.html_safe, 
                                     user_facebook_omniauth_authorize_path, 
                                     class: "form_btn btn btn-primary btn-lg btn-xlarge" %> <br />
          <% end -%>**                  <!-- End - OmniAuth If Statement -->
        </div>                        <!-- End - Signup with Facebook Button -->
      </div>                 <!-- End - Create Account Button Row (FB) -->
    </div>                  <!-- End - Signup Form -->
<% end %> 

我的浏览器和终端错误: enter image description here enter image description here

1 个答案:

答案 0 :(得分:2)

在您的Devise初始化程序(config/initializers/devise.rb)中,您需要使用info_fields参数添加您希望Facebook返回的项目。 Facebook默认只返回姓名,电子邮件和ID。例如:

# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], 
   callback_url: 'https://ossemble-workspace-twistedben.c9users.io/users/auth/facebook/callback',
   info_fields: 'email,name,birthday,locale,gender'

您可以获取Facebook用户对象here上可用的项目列表。某些项目(例如生日)需要用户的许可以及Facebook对您的应用程序的特别批准。您需要在文档中彻底探讨这一点。

此外,Facebook用户可以拒绝您的应用访问他/她个人资料的任何部分。仅仅因为您的请求来自Facebook的电子邮件,并不保证回电将包含电子邮件。您的应用需要优雅地处理这些情况。

在您的情况下,您可能想要创建新的用户记录,但不能立即保留它。相反,将该对象传递给注册表单,该表单允许用户填写他/她的个人资料中的空白。或者您可以保存新记录而无需验证(user.save(validate:false))。

另外,我建议将您的应用ID和应用密码存储在环境变量(ENV[...])中,而不是将其检入源代码或在线发布。你会想立即获得一个新的Facebook秘密。