I'm building a small app to test adal cordova and wanted to create an app with start page with button to login in Azure, but also silent sign-in when token is already in cache. I took the Cordova-Calendar-App-Code-Sample with O365 authentication as basis for my app.
The button with login works fine, but I don't get the silent login, triggered by the controller at startup working. I get the error:
ReferenceError: Microsoft is not defined
at Object.login (file:///android_asset/www/app/service-AAD.js:42:35)
at signInCtrl.vm.silentsignin (file:///android_asset/www/app/auth/sign-in-ctrl.js:24:23)
==> service-AAD.js: authContext = new Microsoft.ADAL.AuthenticationContext(authority);
I thing that the button click login works because of the $scope, and that the silent sign-in is not working because of the 'var vm = this' and missing scope to find the ADAL plugin?
Unfortunately I don't know how to solve this, i hope you can give me a helping hand.
(function () {
'use strict';
angular.module('appAAD').controller('signInCtrl', ['$scope', '$state', 'appAADapi', signInCtrl]);
function signInCtrl($scope, $state, appAADapi) {
var vm = this;
// Callback after successful login
var onlogin = function (reason) {
if (typeof reason != 'undefined') {
if (reason.statusCode === 'Succeeded') {
// Navigate to main page when sign-in was successful.
$state.go('app');
}
}
};
// $scope.signIn is called by button on sign-in.html
// This works
$scope.signIn = function () {
appAADapi.login(onlogin);
};
// This does not work, error:
// ReferenceError: Microsoft is not defined
vm.silentsignin = function () {
appAADapi.loginSilently(onlogin);
}
// Try to sign silently on app startup
// if not successful, then show login button
vm.silentsignin();
}
})();
(function () {
'use strict';
angular.module('appAAD').factory('appAADapi', [appAADapi]);
function appAADapi() {
var authContext;
var authority = "https://login.windows.net/common",
redirectUri = {redirectUri}",
resourceUri = "https://graph.windows.net",
clientId = "{clientId}",
graphApiVersion = "2013-11-08";
function loginSilently(callback) {
if (!authContext) {
authContext = new Microsoft.ADAL.AuthenticationContext(authority);
}
authContext.tokenCache.readItems().then(function (items) {
if (items.length > 0) {
authority = items[0].authority;
authContext = new Microsoft.ADAL.AuthenticationContext(authority);
}
// Attempt to authorize user silently
authContext.acquireTokenSilentAsync(resourceUri, clientId)
.then(callback, function () {
error("No auth tokens availiable, need to login");
});
}, function (err) {
error("Failed to login: " + pre(err));
});
}
function login(callback) {
if (!authContext) {
authContext = new Microsoft.ADAL.AuthenticationContext(authority);
}
authContext.tokenCache.readItems().then(function (items) {
if (items.length > 0) {
authority = items[0].authority;
authContext = new Microsoft.ADAL.AuthenticationContext(authority);
}
// Attempt to authorize user silently
authContext.acquireTokenSilentAsync(resourceUri, clientId)
.then(callback, function () {
// We require user credentials so triggers authentication dialog
authContext.acquireTokenAsync(resourceUri, clientId, redirectUri)
.then(callback, function (err) {
error("Failed to authenticate: " + err);
});
});
}, function (err) {
error("Failed to login: " + pre(err));
});
};
return {
login: login,
loginSilently: loginSilently,
};
};
})();