Auth0 Home Blog Docs

Auth0.js Popup Autoclose

auth0js
popup
auth0-js

#1

Can some post an example of using Auth0.js Popup (social/webAuth.popup.authorize) that autocloses after authorization?


#2

The webAuth.popup.authorize() section in the documentation could indeed have additional information on how to accomplish that so I logged a request to review that section.

However, while that is not completed you should be able to accomplish your requirements by doing something like the following.

Trigger the popup with the necessary options which include a popup specific callback handler and pass a callback function that will then process the data coming from the popup:

var webAuth = new auth0.WebAuth({ domain: "[domain]", clientID: "[client_id]", });

var options = {
    nonce: "[nonce]", // if response type requires it
    redirectUri: "https://[client_app_domain]/callback/popup.html",
    // ... other options like which connection to use
};

webAuth.popup.authorize(options, function (error, response) {
    // ...
});

Implement logic in the popup handler to signal the arrival of a response to the parent page (which closes the popup):

<script type="text/javascript">
    var webAuth = new auth0.WebAuth({ domain: "[domain]", clientID: "[client_id]", });

    webAuth.popup.callback({
        nonce: "[nonce]", // if you passed one when opening popup
    });
</script>

Given the popup handler leads to the popup being closed, the above logic can be included in a very simple HTML page that does not even include the full web application as it will never show any UI if everything goes as planned. It just needs to reference Auth0.js and run some similar logic as demonstrated above.


#3

Thank you for the response @jmangelo. webAuth.popup.callback is what I was looking for. I was originally calling window.close directly, and that was causing the authentication process to fail.


#4

Thank you for the response @jmangelo. webAuth.popup.callback is what I was looking for. I was originally calling window.close directly, and that was causing the authentication process to fail.


#5