Rails + Devise:在公共页面Facebook登录时重定向到特定网址

时间:2011-10-17 13:58:05

标签: ruby-on-rails-3 authentication facebook devise

我有一个可公开评估的页面

"http://www.example.com/gift/5"

上面有一个Facebook连接按钮。

我希望用户点击Facebook连接按钮,然后重定向到

"http://www.example.com/gift/5/coupon"

目前Devise / Omniauth似乎在对用户进行身份验证时丢失了会话。 我在尝试FB.api之前尝试发送ajax请求

    FB.login(function(response) {
    if (response.session) {
       $.ajax({
            url : '/ajax/facebook/url',
            type : 'post',
            data :  {push_to: "/gift/5/coupon"},
            beforeSend: function(xhr) {
                xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
            }
        });
      window.location = "https://graph.facebook.com/oauth/authorize?client_id=..........";
    } else {

    }
  });

Ajax请求命中

class AjaxController < ApplicationController

  def ajax_facebook_url_redirect

     session[:"fb_redirect_to"] = params[:push_to]

  end

但我无法在

中找到变量
application_controller.rb

def after_sign_in_path_for(resource)
    print session[:"fb_redirect_to"]
    (session[:"user.return_to"].nil?) ? "/" : session[:"user.return_to"].to_s
  end

有关将用户从公共页面发送到特定网址的任何建议吗?

1 个答案:

答案 0 :(得分:0)

最近在Devise网站上的OmniAuth概述文档中添加了一个新部分,解决了这个问题:https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

这是要点:

def authenticate_user!
  if !current_user
    # This should work, but session is lost. See https://github.com/plataformatec/devise/issues/1357
    # session[:return_to] = request.fullpath
    redirect_to user_omniauth_authorize_path(:google_apps, :origin => request.fullpath)
  end
end   

def after_sign_in_path_for(resource)
  # This should work, but session is lost. See https://github.com/plataformatec/devise/issues/1357
  # return_to = session[:return_to]
  # session[:return_to] = nil
  return_to = request.env['omniauth.origin']
  stored_location_for(resource) || return_to || root_path  
end