4

ここの初心者、アプリケーションに omniauth-facebook のセットアップがあり、すべてが正常に機能しています。唯一の問題は、認証後に別のページにリダイレクトしたいのですが、よくわかりません。私の最初の解決策は、auth/facebook/callback を "sessions#new" に一致させるルートを追加することでした。私が試みる他のことは、コントローラーにリダイレクトを追加することですが、機能しません。特定のページにリダイレクトするのは何が適切ですか?

コントローラ

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 , :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "sign in successfuly") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

ルート

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

助けてくれてありがとう。

4

1 に答える 1

7

メソッドでユーザーを取得するfind_for_facebook_oauthと、そのユーザーでログインし、目的のパスに redirect_to を使用できます。次のようなことができます:

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(@user)
      redirect_to desired_path, notice: 'Signed in successfully.'
      set_flash_message(:notice, :success, :kind => "sign in successfuly") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end
于 2013-06-17T16:53:23.963 に答える