[英]Omniauth + Devise Redirect Error
我遇到以下問題:我的應用程序正在作為Facebook應用程序運行,但是在用戶第一次接受它的Facebook權限時,它被重定向到正確的URL,但位於Facebook(畫布)之外。 我該如何解決?
這是我的實際設置:
devise.rb: config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET'], scope: "email,publish_stream"
app / controllers / omniauth_callback_controller.rb
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
def facebook
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to root_path
end
end
def after_sign_in_path_for(resource)
inicio_path
end
end
routes.rb
Pl::Application.routes.draw do
ActiveAdmin.routes(self)
mount Resque::Server, :at => "/resque"
devise_for :admin_users, ActiveAdmin::Devise.config
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
devise_scope :user do
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
end
match 'states' => 'game_plays#states'
match 'cities' => 'game_plays#cities'
match 'parties' => 'game_plays#parties'
match 'prefeito' => 'game_plays#prefeito'
match 'prefeitos' => 'game_plays#prefeitos'
match 'vereadores' => 'game_plays#vereadores'
match 'parties' => 'game_plays#parties'
match 'qualities' => 'game_plays#qualities'
match 'start' => 'game_plays#start'
match 'generated_image' => 'game_plays#generated_image'
match 'save_game_play' => 'game_plays#save_game_play'
match 'final' => 'game_plays#final', :as => :final
match 'inicio' => 'game_plays#index'
root :to => 'game_plays#bem_vindo'
end
有任何建議嗎?
這是根據我幾個月前對這個問題的經驗得出的,希望對您有所幫助。
從Facebook的角度來看,這正是應該做的。 它無法識別請求來自何處。 您必須更改請求中的重定向URL,以使其保留在iframe中。
我發現最簡單的方法是將iframe網址轉到my_website.com/fb-app,該網址設置了一個會話變量,讓您知道自己在畫布中,並確保它們已經通過facebook登錄。 然后,將它們重定向到畫布站點,該站點將繼續正常運行,並且用戶從不注意任何內容。
def fbapp
session[:fbapp] = true
if user_signed_in? == false
redirect_to user_omniauth_authorize_path(:facebook)
else
redirect_to $APP_CANVAS_URL + "?auth_token=" + current_user.authentication_token
end
end
然后,在Application_controller.rb中,您需要更改重定向路徑。 這就是我的應用程序中的樣子,請注意,$ APP_CANVAS_URL應該類似於https://apps.facebook.com/your_app_here
def after_sign_in_path_for(resource_or_scope)
if session[:fbapp] == true
$APP_CANVAS_URL + "?auth_token=" + current_user.authentication_token
else
root_path
end
end
我懷疑這是最好的方法,但這是經過數小時的挫折之后我必須為我工作的方法。 希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.