Skip to content

Instantly share code, notes, and snippets.

@Hihaj

Hihaj/adal-app.js

Last active Oct 24, 2019
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@georgeslegros georgeslegros commented Mar 10, 2016

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

This comment has been minimized.

Copy link

@muralikrrish279 muralikrrish279 commented Jun 25, 2016

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

This comment has been minimized.

Copy link

@mmakwana mmakwana commented Mar 22, 2018

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

This comment has been minimized.

Copy link

@mmakwana mmakwana commented Mar 22, 2018

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

This comment has been minimized.

Copy link

@tapsbops 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