Closing OAuth 2.0 popup window after redirect
Asked Answered
T

3

34

I redirect user to the OAuth 2.0 authorization endpoint in popup window. What is best way to close this popup and refresh main window after OAuth 2.0 authorization server redirects user back with an authorization code?

Thanks in advance for any help.

Thickness answered 29/11, 2011 at 10:53 Comment(0)
F
39

I think popup you can close by

parent.close();

And to refresh main window I used this trick:

$(function() {
    var win;
    var checkConnect;
    var $connect = $("#some_button");
    var oAuthURL = "http://example.com/account/_oauth?redirect_url=" + redirect_url;
    $connect.click(function() {
        win = window.open(oAuthURL, 'SomeAuthentication', 'width=972,height=660,modal=yes,alwaysRaised=yes');
    });

    checkConnect = setInterval(function() {
        if (!win || !win.closed) return;
        clearInterval(checkConnect);
        window.location.reload();
    }, 100);
});

Opener ( main window ) just checks every time if the popup still live and if win.closed returns true - the main window reloads

Hope it will help somebody

Fortunia answered 12/3, 2012 at 11:50 Comment(1)
Not working in my case win.closed never changes the status. Solution was to use the win.name property.Eulogy
F
2

So IDK if this will help anyone, but instead of refreshing the page, you probably want to be checking your server to see if the user connected or not via a re-occurring timeout, and then use .close() after they did. An added benefit here is that you'll now have the data to continue on with the registration process.

Fant answered 11/12, 2018 at 1:56 Comment(0)
S
0

First, on the main window you need to open an new popup window, this will open a centered window:

$('.openwindow').click(function(){
    var uri = 'https://my-oauth-server-url'
    var left = (screen.width/2 - 400)
    var top = (screen.height/2 - 350)
    window.open(uri, 'oauth window','resizable=yes, width=800, height=700, top='+top+', left='+left)
});

Once you have the popup window opened you will need to reload the main window (opener window) with new content and close the popup window.

window.opener.location.replace('/oauthok');
window.close();
Spall answered 9/5, 2022 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.