How to make Omniauth work with a popup window?
-
08-10-2019 - |
Question
The default for Omniauth works beautifully if you are opening it in the existing window, but not sure how it works in a popup context, and you are handling a large part of the interactions via javascript
Solution
Seems like you could do this yourself?
When user clicks 'login via facebook' - use JS to pop open a window with a location of /auth/facebook. The 'callback' will just route back to /auth/callback in this same window. Once you've done your work with the callback, close the current window and refresh the parent?
OTHER TIPS
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;
}
Replace provider_url with '/auth/facebook' or '/auth/twitter' should work.
At your callback url,
<script type="text/javascript">
window.opener.location = '<%= @redirect_to %>';
window.close();
</script>
You can refresh the page however you want with @redirect_to pointing to the page where your parent window should be refreshed to.