2013-05-02 75 views
0

目前我的應用程序設置爲使用Omniauth和Devise與Twitter和Facebook。我的問題是,當用戶在Twitter上按下取消或Facebook不授權它將我發回到devises/users/sign_in鏈接。問題是我需要它回到root_url,但我沒有看到在哪裏定義。來自Omniauth的錯誤重定向/設計失敗

在我User.rb我有

devise :omniauthable, :omniauth_providers => [:facebook, :twitter] 

,在我的routes.rb我有

devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"} 

那麼,有沒有一種簡單的方法來解決它重定向到?我一直在使用谷歌搜索,並沒有能夠看到一個合適的解決方案,雖然它可能在我面前。

此外,在這個應用程序,我們永遠不希望用戶看到設計會議#新頁面。有沒有一種方法可以禁用它?

在此先感謝...

回答

-1

要做到這一點,您將實現自己的回調。

在配置/ routes.rb中告訴設計在控制我們的回調是:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } 

創建文件 「app/controllers/users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    # Create a method with the same name as your provider, 
    # def facebook, def twitter 
    def facebook 
    # Check if user authenticated with your facebook application or not. 
    # If not redirect to root_url 
    end 
end 

希望這給你的指導方針。

在配置
1

/routes.rb中

root :to => 'your_home_controller#your_home_page' 
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } 
在app /控制器/用戶/ omniauth_callbacks_controller.rb

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.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) 

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 root_path**** 
end 
end 

end