@@ -397,13 +397,7 @@ api.ajax.jsonrpc = {};
397397 * @param {* } pHideSuccessErrorModal
398398 */
399399
400- api . ajax . jsonrpc . request = function ( pAPI_URL , pAPI_Method , pAPI_Params , callbackFunctionName_onSuccess , callbackParams_onSuccess , callbackFunctionName_onError , callbackParams_onError , pAJAX_Params , pItemSpinner = null , pHideSuccessErrorModal = false ) {
401- //call the async function to handle catch block of acquiretokensilently
402- api . ajax . jsonrpc . processRequestAsync ( pAPI_URL , pAPI_Method , pAPI_Params , callbackFunctionName_onSuccess , callbackParams_onSuccess , callbackFunctionName_onError , callbackParams_onError , pAJAX_Params , pItemSpinner , pHideSuccessErrorModal )
403- }
404-
405-
406- api . ajax . jsonrpc . processRequestAsync = async function ( pAPI_URL , pAPI_Method , pAPI_Params , callbackFunctionName_onSuccess , callbackParams_onSuccess , callbackFunctionName_onError , callbackParams_onError , pAJAX_Params , pItemSpinner = null , pHideSuccessErrorModal = false ) {
400+ api . ajax . jsonrpc . request = async function ( pAPI_URL , pAPI_Method , pAPI_Params , callbackFunctionName_onSuccess , callbackParams_onSuccess , callbackFunctionName_onError , callbackParams_onError , pAJAX_Params , pItemSpinner = null , pHideSuccessErrorModal = false ) {
407401 // Default API parameters
408402 pAPI_Params = pAPI_Params || { } ;
409403
@@ -525,7 +519,7 @@ api.ajax.jsonrpc.processRequestAsync = async function (pAPI_URL, pAPI_Method, pA
525519
526520 try {
527521 //force to wait for return
528- const token = await api . sso . acquireAccessTokenSilently ( pAPI_Method ) ;
522+ const token = await api . sso . acquireAccessTokenSilently ( ) ;
529523
530524 if ( token ) {
531525 headers [ "MSAL" ] = `Bearer ${ token } ` ;
@@ -1091,8 +1085,6 @@ api.sso.msalConfigObj = {};
10911085api . sso . msalInstance = null ;
10921086api . sso . tokenPromise = null ;
10931087
1094- //temporary, to be removed
1095- api . sso . response = null ;
10961088
10971089api . sso . authenticateUserSilently = function ( callbackFunctionName_onSuccess , callbackFunctionName_onError , hideLoginPopup ) {
10981090 api . spinner . start ( ) ;
@@ -1102,31 +1094,19 @@ api.sso.authenticateUserSilently = function (callbackFunctionName_onSuccess, cal
11021094 let loginRequest = {
11031095 scopes : [ "api://" + api . sso . msalConfigObj . auth . clientId + "/access_application" ]
11041096 } ;
1105-
11061097 api . sso . msalInstance . ssoSilent ( loginRequest ) . then ( ( response ) => {
11071098 api . spinner . stop ( ) ;
1108- //User logged in successfully
1109-
1110- console . log ( response . accessToken )
1111- /* console.log("expiresOn : " + response.expiresOn);
1112- console.log("extExpiresOn : " + response.extExpiresOn);
1113- console.log("FIRST");
1114- */
1115-
1116- api . sso . response = response ;
1117-
11181099
11191100 api . ajax . callback ( callbackFunctionName_onSuccess , response ) ;
11201101 } ) . catch ( error => {
11211102 api . spinner . stop ( ) ;
1122- console . log ( "Silent Error: " + error ) ;
11231103
11241104 if ( hideLoginPopup ) {
11251105 //Error during login
11261106 if ( callbackFunctionName_onError != null ) {
11271107 api . ajax . callback ( callbackFunctionName_onError , error ) ;
11281108 }
1129- } else if ( error instanceof msal . InteractionRequiredAuthError ) {
1109+ } else if ( ( error instanceof msal . InteractionRequiredAuthError || error . errorCode === 'monitor_window_timeout' ) && ! hideLoginPopup ) {
11301110 //if the error indicates interaction is required, fallback to login
11311111 console . log ( "SSO Silent requires interation. prompting login...." )
11321112 api . sso . authenticateUserPopup ( callbackFunctionName_onSuccess , callbackFunctionName_onError ) ;
@@ -1146,7 +1126,6 @@ api.sso.authenticateUserSilently = function (callbackFunctionName_onSuccess, cal
11461126 * @param {* } callbackFunctionName_onError
11471127 */
11481128api . sso . authenticateUserPopup = function ( callbackFunctionName_onSuccess , callbackFunctionName_onError ) {
1149- console . log ( "authenticateUserPopup" ) ;
11501129 let loginRequest = {
11511130 scopes : [ "api://" + api . sso . msalConfigObj . auth . clientId + "/access_application" ]
11521131 } ;
@@ -1155,15 +1134,14 @@ api.sso.authenticateUserPopup = function (callbackFunctionName_onSuccess, callba
11551134 . then ( ( response ) => {
11561135 //User logged in successfully
11571136 api . spinner . stop ( ) ;
1158- console . log ( response . accessToken )
1159- //// api.idle.startIdleTimer();
11601137 api . ajax . callback ( callbackFunctionName_onSuccess , response ) ;
11611138 } )
11621139 . catch ( error => {
11631140 api . spinner . stop ( ) ;
1164- console . log ( "login failed:" , error ) ;
11651141 //Error during login
1166- api . ajax . callback ( callbackFunctionName_onError , error ) ;
1142+ if ( callbackFunctionName_onError != null ) {
1143+ api . ajax . callback ( callbackFunctionName_onError , error ) ;
1144+ }
11671145 } ) ;
11681146}
11691147
@@ -1173,19 +1151,16 @@ api.sso.authenticateUserPopup = function (callbackFunctionName_onSuccess, callba
11731151 * @param {* } callbackFunctionName_onError
11741152 */
11751153api . sso . authenticateUserPopupAsync = async function ( ) {
1176- console . log ( "authenticateUserPopup" ) ;
11771154 let loginRequest = {
11781155 scopes : [ "api://" + api . sso . msalConfigObj . auth . clientId + "/access_application" ]
11791156 } ;
11801157
11811158 try {
11821159 return await api . sso . msalInstance . loginPopup ( loginRequest ) ;
11831160 } catch ( error ) {
1184- console . log ( "login failed:" , error ) ;
11851161 // Error during login
11861162 return null ;
11871163 }
1188-
11891164}
11901165
11911166/**
@@ -1220,10 +1195,7 @@ api.sso.logout = function (callbackFunctionName_onSuccess, callbackFunctionName_
12201195 * check if the user has an entra account and retrieve token if present
12211196 * @param {* } pAPI_Method
12221197 */
1223- api . sso . acquireAccessTokenSilently = function ( pAPI_Method ) {
1224-
1225- console . log ( "get access token silently" ) ;
1226-
1198+ api . sso . acquireAccessTokenSilently = async function ( ) {
12271199 const validAccount = api . sso . getMsalAccount ( ) ;
12281200
12291201 if ( validAccount ) {
@@ -1234,9 +1206,7 @@ api.sso.acquireAccessTokenSilently = function (pAPI_Method) {
12341206 return null ; // Promise.reject("No account signed in");
12351207 }
12361208
1237- console . log ( api . sso . tokenPromise ) ;
1238-
1239- // If token request is already in progress, reuse it
1209+ // If token request is already in progress, reuse it -- sharing the same promise
12401210 if ( api . sso . tokenPromise ) return api . sso . tokenPromise ;
12411211
12421212 let scope = [ "api://" + api . sso . msalConfigObj . auth . clientId + "/access_application" ] ;
@@ -1257,10 +1227,7 @@ api.sso.acquireAccessTokenSilently = function (pAPI_Method) {
12571227 forceRefresh : true
12581228 } ) . then ( ( response ) => {
12591229 // User is authenticated, return access token
1260- api . sso . tokenPromise = null ; // clear cache
1261- console . log ( response . expiresOn ) ;
1262- console . log ( "finished2 =>" + pAPI_Method ) ;
1263-
1230+ api . sso . tokenPromise = null ; // clear cache i.e. mark the promise as complete
12641231 return response . accessToken ;
12651232 } ) . catch ( async ( error ) => {
12661233 if ( error instanceof msal . InteractionRequiredAuthError ) {
@@ -1277,7 +1244,6 @@ api.sso.acquireAccessTokenSilently = function (pAPI_Method) {
12771244 }
12781245 } ) ;
12791246
1280- console . log ( "finished1 =>" + pAPI_Method ) ;
12811247 return api . sso . tokenPromise ;
12821248}
12831249
@@ -1302,4 +1268,24 @@ api.sso.getMsalAccount = function () {
13021268 } ) ;
13031269
13041270 return validAccount ;
1305- }
1271+ }
1272+
1273+ /**
1274+ * create the msal instance for the applciation
1275+ * @param {* } callbackFunctionName_onSuccess
1276+ * @param {* } callbackFunctionName_onError
1277+ */
1278+ api . sso . createMsalInstance = function ( callbackFunctionName_onSuccess , callbackFunctionName_onError ) {
1279+ debugger ;
1280+ msal . PublicClientApplication . createPublicClientApplication ( api . sso . msalConfigObj )
1281+ . then ( ( obj ) => {
1282+ api . sso . msalInstance = obj ;
1283+ api . ajax . callback ( callbackFunctionName_onSuccess ) ;
1284+ } )
1285+ . catch ( ( error ) => {
1286+ if ( callbackFunctionName_onError ) {
1287+ api . ajax . callback ( callbackFunctionName_onError , error ) ;
1288+ }
1289+ console . log ( "Error creating MSAL PublicClientApplication:" , error ) ;
1290+ } ) ;
1291+ }
0 commit comments