Skip to content

Instantly share code, notes, and snippets.

@fjeldstad
Last active October 24, 2019 15:57
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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.
@georgeslegros
Copy link

Hi There! This saved me! Thanks!
One note maybe, not sure which version of adal.js you are using but handleWindowCallback does not accept the hash in the latest version.

However I modified my version of adal.js and that seem to work fine.

@muralikrrish279
Copy link

Is this solution working in IE?

This perfectly works fine in Chrome however in IE 'popup.location.pathname' check is always errors out as popup.location is undefined.

@mmakwana
Copy link

Worked fine in Chrome but in IE 11 it is always giving Error ("undefined") on this line:
#29 if (popup.location.pathname.indexOf('/' + dummyAuthPage) >= 0) {

Also, in IE 11 it is giving another error "Access denied" for the url which i am using as "redirect url" . any solution to this?

@mmakwana
Copy link

anything which says " Promise " is giving error "undefined". e.g. Promise, reject(), resolve() are giving same error.
do i need to include some external java-script library ? can u provide WORKING SAMPLE please ?

@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