我正在设计omniauth使用facebook验证用户身份。我使用文档https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview实现了它 它工作不正常。当我使用我的Facebook帐户登录时,它会把我带到"失败"回调控制器中的方法。 但有趣的是,当我打开Facebook时,它打开没有任何问题意味着它正在验证我的帐户。 我通过登录和从facebook注销多次检查了它。但问题是,如果它是验证Facebook帐户,那么为什么它需要我去#34;失败方法"。 这是我的代码
回调控制器。
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
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
render plain: params.inspect
# redirect_to root_path
end
end
用户模型
class User < ApplicationRecord
# Include default devise modules. Others available are:
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
devise :omniauthable, :omniauth_providers => [:facebook]
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.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end
end
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
end
路线代码
devise_for :users, controllers: {confirmations: 'confirmations',registrations: 'users/registrations',omniauth_callbacks: 'users/omniauth_callbacks' } do
#put "confirm_user", to: "confirmations#confirm_user"
get "confirmation", to: "confirmations#after_confirmation_path_for"
end
Facebook应用设置截图
https://www.dropbox.com/s/vn8cjpr5wyagkdp/Screenshot%202017-11-27%2017.04.28.png?dl=0
登录后开发日志
Started GET "/users/auth/facebook" for 10.0.2.2 at 2017-11-28 14:13:37 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Started GET "/users/auth/facebook/callback?code=AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8" for 10.0.2.2 at 2017-11-28 14:13:40 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::OmniauthCallbacksController#failure as HTML
Parameters: {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"}
Rendering text template
Rendered text template (0.0ms)
Completed 200 OK in 20ms (Views: 10.6ms | ActiveRecord: 0.0ms)
params.inspect的结果如下
<ActionController::Parameters {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"} permitted: false>
rake路线的输出是
rake routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) users/omniauth_callbacks#passthru
user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) users/omniauth_callbacks#facebook
user_twitter_omniauth_authorize GET|POST /users/auth/twitter(.:format) users/omniauth_callbacks#passthru
user_twitter_omniauth_callback GET|POST /users/auth/twitter/callback(.:format) users/omniauth_callbacks#twitter
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
user_registration PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST /users(.:format) users/registrations#create
new_user_confirmation GET /users/confirmation/new(.:format) confirmations#new
user_confirmation GET /users/confirmation(.:format) confirmations#show
POST /users/confirmation(.:format) confirmations#create
new_model_session GET /models/sign_in(.:format) devise/sessions#new
model_session POST /models/sign_in(.:format) devise/sessions#create
destroy_model_session DELETE /models/sign_out(.:format) devise/sessions#destroy
new_model_password GET /models/password/new(.:format) devise/passwords#new
edit_model_password GET /models/password/edit(.:format) devise/passwords#edit
model_password PATCH /models/password(.:format) devise/passwords#update
PUT /models/password(.:format) devise/passwords#update
POST /models/password(.:format) devise/passwords#create
cancel_model_registration GET /models/cancel(.:format) devise/registrations#cancel
new_model_registration GET /models/sign_up(.:format) devise/registrations#new
edit_model_registration GET /models/edit(.:format) devise/registrations#edit
model_registration PATCH /models(.:format) devise/registrations#update
PUT /models(.:format) devise/registrations#update
DELETE /models(.:format) devise/registrations#destroy
POST /models(.:format) devise/registrations#create
new_model_confirmation GET /models/confirmation/new(.:format) devise/confirmations#new
model_confirmation GET /models/confirmation(.:format) devise/confirmations#show
POST /models/confirmation(.:format) devise/confirmations#create
donations_donor_history GET /donations/donor_history(.:format) donations#donor_history
donations_donor_signup GET /donations/donor_signup(.:format) donations#donor_signup
POST /donations/donor_signup(.:format) donations#donor_signup
donations_sms_service POST /donations/sms_service(.:format) donations#sms_service
donations_create_user_account POST /donations/create_user_account(.:format) donations#create_user_account
donations_add_user_payroll POST /donations/add_user_payroll(.:format) donations#add_user_payroll
donations GET /donations(.:format) donations#index
edit_donation GET /donations/:id/edit(.:format) donations#edit
donation GET /donations/:id(.:format) donations#show
PATCH /donations/:id(.:format) donations#update
PUT /donations/:id(.:format) donations#update
DELETE /donations/:id(.:format) donations#destroy
campaign_donations POST /campaigns/:campaign_id/donations(.:format) donations#create
new_campaign_donation GET /campaigns/:campaign_id/donations/new(.:format) donations#new
campaign_donations_create_user_account GET /campaigns/:campaign_id/donations/create_user_account(.:format) donations#create_user_account
campaigns GET /campaigns(.:format) campaigns#index
POST /campaigns(.:format) campaigns#create
new_campaign GET /campaigns/new(.:format) campaigns#new
edit_campaign GET /campaigns/:id/edit(.:format) campaigns#edit
campaign GET /campaigns/:id(.:format) campaigns#show
PATCH /campaigns/:id(.:format) campaigns#update
PUT /campaigns/:id(.:format) campaigns#update
DELETE /campaigns/:id(.:format) campaigns#destroy
organizations GET /organizations(.:format) organizations#index
POST /organizations(.:format) organizations#create
new_organization GET /organizations/new(.:format) organizations#new
edit_organization GET /organizations/:id/edit(.:format) organizations#edit
organization GET /organizations/:id(.:format) organizations#show
PATCH /organizations/:id(.:format) organizations#update
PUT /organizations/:id(.:format) organizations#update
DELETE /organizations/:id(.:format) organizations#destroy
admins_social_sharing_switch POST /admins/social_sharing_switch(.:format) admins#social_sharing_switch
admins_error_detail GET /admins/error_detail(.:format) admins#error_detail
GET /admins/generate_report/:id(.:format) admins#generate_report
admins_create_company GET /admins/create_company(.:format) admins#create_company
POST /admins/create_company(.:format) admins#create_company
admins_revenue_detail GET /admins/revenue_detail(.:format) admins#revenue_detail
admins_create_account GET /admins/create_account(.:format) admins#create_account
admins_view_account GET /admins/view_account(.:format) admins#view_account
GET /admins/view_company/:id(.:format) admins#view_company
admins_donation_analysis GET /admins/donation_analysis(.:format) admins#donation_analysis
admins_link_expiry GET /admins/link_expiry(.:format) admins#link_expiry
admins_edit_profile GET /admins/edit_profile(.:format) admins#edit_profile
admins_update_profile POST /admins/update_profile(.:format) admins#update_profile
POST /admins/create_account(.:format) admins#create_account
admin_destroy GET /admins/:id(.:format) admins#destroy
admins GET /admins(.:format) admins#index
POST /admins(.:format) admins#create
new_admin GET /admins/new(.:format) admins#new
edit_admin GET /admins/:id/edit(.:format) admins#edit
admin GET /admins/:id(.:format) admins#show
PATCH /admins/:id(.:format) admins#update
PUT /admins/:id(.:format) admins#update
DELETE /admins/:id(.:format) admins#destroy
crons_expirylink_alert GET /crons/expirylink_alert(.:format) crons#expirylink_alert
users_sign_out GET /users/sign_out(.:format) devise/sessions#destroy
root GET / campaigns#latest
答案 0 :(得分:1)
经过2天的努力,我终于自己解决了这个问题。除了宝石本身,每件事都很好。如果你只是写gem omniauth-facebook
它会安装版本1.4.0,这是非常古老的。我将它更新为gem 'omniauth-facebook', '~> 4.0'
,它就像一个魅力。因此,如果有人面临同样的问题,他应该更新宝石。