当某些链接点击时,强制通过弹出弹出登录,然后向前友好



遵循此Q&一个线程,我已经知道了它,以便用户可以使用弹出窗口通过Omniauth Facebook策略登录。这是我的代码:

查看:

<%= link_to("Sign in with Facebook", "/auth/facebook", :id => "signin", 
:data => {:width => 600, :height => 400}) %>

application.js:

function popupCenter(url, width, height, name) {
  var left = (screen.width/2)-(width/2);
  var top = (screen.height/2)-(height/2);
  return window.open(url, name, "menubar=no,toolbar=no,status=no,width="+width+",height="+height+",toolbar=no,left="+left+",top="+top);
}
$(document).ready(function() {
  $('a#signin').click(function(e) {
    popupCenter($(this).attr('href'), $(this).attr('data-width'), $(this).attr('data-height'), 'authPopup');
    e.stopPropagation();
    return false;
  });
});

sessions_controller.rb:

  def create
    user = AuthProviders::FacebookUser.find_or_create_user_from(auth_hash)
    session[:current_user_id] = user.id
    @return_to = origin || root_url
    render :callback, :layout => false
  end
  protected
  def auth_hash
    request.env['omniauth.auth']
  end
  def origin
    request.env['omniauth.origin']
  end

views/sessions/callback.html.erb:

<script type="text/javascript">
  if (window.opener) {
    window.opener.location = '<%= @return_to %>';
  }
  else {
    window.location = '<%= @return_to %>';
  }
  return window.close();
</script>

这一切都很好,但是我需要代码才能处理两个其他方案:

(1)用户单击链接和控制器操作会呈现一个普通的旧HTML响应,但是只有在有current_user(即签名)时才能查看该页面。单击链接后,用户被迫使用弹出窗口通过Facebook登录。经过身份验证后,将用户转发到原始请求页。

<%= active_link_to("Your Stuff", user_stuff_path(current_user), :wrap_tag => :li) %>

当您不使用弹出窗口时,使用Hartly的友好转发章就很容易做到这一点。

(2)其他方案类似于(1),但是Controller Action呈现JavaScript而不是HTML,然后将(Twitter Bootstrap)模态对话框打开。用户必须是登录以查看此模式框,因此该应用需要向用户提供Facebook auth对话框,然后将其转发回到模态框。这是我的应用程序中的示例链接:

<%= link_to(event.home_pick_path, :remote => true, :class => "#{event.home_button_class}", :rel => "tooltip", :data => {:placement => "top", "original-title" => "#{event.home_bet_tooltip}"}) do %>
  <div class="team-name"><%= event.home_team_nickname %></div>
<% end %>

请求URL看起来像这样:/picks/new?pick_line_id=1&pick_num=1

所以我想象到这两种情况的解决方案是什么,涉及一些管道,因此我感谢您的时间提前回应。

我认为您已经超越了工程。我会选择一种非常简单的方法,但是当然,这取决于您的业务逻辑。

如果没有当前用户,我会在带有"登录"按钮的同一URL" A 401"模板中渲染,Wich会触发FB登录按钮。

在回调中(未测试)..

def create
  if session[:redirect_to].nil?
    session[:redirect_to] = origin
  end
  user = AuthProviders::FacebookUser.find_or_create_user_from(auth_hash)
  session[:current_user_id] = user.id
  unless session[:redirect_to].nil?
    redirect = session[:redirect_to] || root_url
    session[:redirect_to] = nil
    redirect_to redirect
  end 
end

当然,由于您不需要它,没有模板可以处理它。

最新更新