Skip to content

Instantly share code, notes, and snippets.

@shwetayadkikar
Last active June 20, 2017 07:03
Show Gist options
  • Save shwetayadkikar/7c0a7811b12a7e006f2dff8f0c19004e to your computer and use it in GitHub Desktop.
Save shwetayadkikar/7c0a7811b12a7e006f2dff8f0c19004e to your computer and use it in GitHub Desktop.
window.accessToken = null;
window.tokenDefer = $.Deferred();
var adLoginService = (function ($) {
$(document).ready(function () {
console.log("doc ready from AD Login");
window.config = {
clientId: '<clientId of the azure AD app>',
postLogoutRedirectUri: window.location.origin,
cacheLocation: 'localStorage' // enable this for IE, as sessionStorage does not work for localhost.
};
var authContext = new AuthenticationContext(config);
var isCallback = authContext.isCallback(window.location.hash);
authContext.handleWindowCallback();
// if (isCallback && !authContext.getLoginError()) {
// window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
//}
// If not logged in force login
var cachedToken = authContext.getCachedToken(window.config.clientId);
if (cachedToken) {
console.log("user already logged in");
// Logged in already
authContext.acquireToken(authContext.config.loginResource, function (error, token) {
if (error || !token) {
console.log("ADAL error occurred: " + error);
window.tokenDefer.reject();
}
console.log("got the token.. resolving tokendefer");
window.accessToken = token;
window.tokenDefer.resolve(token);
});
}
else {
// NOTE: you may want to render the page for anonymous users and render
// a login button which runs the login function upon click.
console.log("calling login");
authContext.login();
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment