身份验证后将omniauth-facebook重定向到特定页面

时间:2013-06-17 13:46:57

标签: ruby-on-rails omniauth

新手在这里,我在应用程序中设置了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" }

感谢您的帮助。

1 个答案:

答案 0 :(得分:6)

当您通过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