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 回答