如何让Omniauth使用弹出窗口?

时间:2010-12-05 09:18:55

标签: ruby-on-rails authentication oauth twitter-oauth omniauth

如果您在现有窗口中打开它,Omniauth的默认设置可以很好地工作,但不确定它在弹出环境中是如何工作的,并且您通过javascript处理大部分交互

2 个答案:

答案 0 :(得分:15)

var newwindow;
function login(provider_url, width, height) {
  var screenX     = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft,
      screenY     = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
      outerWidth  = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth,
      outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22),
      left        = parseInt(screenX + ((outerWidth - width) / 2), 10),
      top         = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
      features    = ('width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);

      newwindow = window.open(provider_url, 'Login', features);

  if (window.focus)
    newwindow.focus();

  return false;
}

将provider_url替换为'/ auth / facebook'或'/ auth / twitter'应该有效。

在您的回调网址

<script type="text/javascript">
  window.opener.location = '<%= @redirect_to %>';
  window.close();
</script>

您可以随意刷新页面,@redirect_to指向应该刷新父窗口的页面。

答案 1 :(得分:3)

好像你自己可以做到这一点?

当用户点击“通过Facebook登录”时 - 使用JS弹出一个位置为/ auth / facebook的窗口。 'callback'将在同一窗口中返回/ auth / callback。使用回调完成工作后,关闭当前窗口并刷新父级?