Skip to content

Instantly share code, notes, and snippets.

@fjeldstad
Last active October 24, 2019 15:57
Show Gist options
  • Save fjeldstad/6e77813d730ebf4ebe7c to your computer and use it in GitHub Desktop.
Save fjeldstad/6e77813d730ebf4ebe7c to your computer and use it in GitHub Desktop.
Example usage of login popup with adal.js
var authContext = new AuthenticationContext({
// ...
});
// Our custom asynchronous login function. Uses authContext.config.displayCall
// to show the Azure AD login page in a popup window, then periodically checks
// the popup window for the resulting hash + uses adal.js to handle it.
// For this to work, we need a dummy landing page to use as redirectUri.
// This can be an empty HTML page, but needs to have the same origin as the
// main window.
var dummyAuthPage = 'auth.html';
var getUser = function () {
return new Promise(function (resolve, reject) {
// If the user is cached, resolve the promise immediately.
var user = authContext.getCachedUser();
if (user) {
resolve(user);
return;
}
// The user was not cached. Open a popup window which
// performs the OAuth login process, then signals
// the result.
authContext.config.displayCall = function (url) {
authContext.config.displayCall = null;
var popup = window.open(url, 'auth-popup', 'width=800,height=500');
var intervalId = window.setInterval(function () {
try {
if (popup.location.pathname.indexOf('/' + dummyAuthPage) >= 0) {
window.clearInterval(intervalId);
authContext.handleWindowCallback(popup.location.hash);
popup.close();
var user = authContext.getCachedUser();
if (user) {
resolve(user);
} else {
reject(authContext.getLoginError());
}
}
} catch (whatever) {
if (popup.closed) {
reject();
}
}
}, 100);
};
authContext.config.redirectUri = window.location.href.replace('index.html', '') + dummyAuthPage;
authContext.login();
});
};
// Now use getUser() whenever a user token is needed.
@tapsbops
Copy link

tapsbops commented Apr 25, 2018

i tried the above code on the login page but a popup window is not being displayed

` function Login() {
var dummyAuthPage = 'Login.html';
var getUser = function () {
return new Promise(function (resolve, reject) {
// If the user is cached, resolve the promise immediately.
var user = authContext.getCachedUser();
if (user) {
resolve(user);
return;
}

                authContext.config.displayCall = function (url) {
                    authContext.config.displayCall = null;
                    var popup = window.open(url, 'auth-popup', 'width=800,height=500');
                    var intervalId = window.setInterval(function () {
                        try {
                            if (popup.location.pathname.indexOf('/' + dummyAuthPage) >= 0) {
                                window.clearInterval(intervalId);
                                authContext.handleWindowCallback(popup.location.hash);
                                popup.close();
                                var user = authContext.getCachedUser();
                                if (user) {
                                    resolve(user);
                                } else {
                                    reject(authContext.getLoginError());
                                }
                            }
                        } catch (whatever) {
                            if (popup.closed) {
                                reject();
                            }
                        }
                    }, 100);
                };

                authContext.config.redirectUri = window.location.href.replace('home.html', '') + dummyAuthPage;
                authContext.login();
            });
        };
    }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment