2017-07-20 71 views
2

我想在REDUX中使用MSAL庫進行身份驗證,但遇到一些麻煩。當我僅作出反應並執行相同的操作時,我成功獲取訪問令牌,但試圖在REDUX中使用它,嘗試獲取訪問令牌時總是得到超時。與REDUX一起使用MSAL(Microsoft身份驗證庫)

function Auth() { 
    var userAgentApplication = new Msal.UserAgentApplication(*my app id*, null, function (errorDes, token, error, tokenType) { 
    // this callback is called after loginRedirect OR acquireTokenRedirect (not used for loginPopup/aquireTokenPopup) 
    }); 

    return new Promise((resolve, reject) => { 
     console.log('inside the promise'); 
     userAgentApplication.loginPopup(["user.read"]).then((token) => { 
      console.log("Successfully got id token"); 
      console.log("first token: ", token); 
      console.log(userAgentApplication.getUser().name); 
      userAgentApplication.acquireTokenSilent(["user.read"]).then((token) => { 
      resolve(token); 
     }, function(error) { 
      reject(error); 
     }); 
    }, function (error) { 
     reject(error); 
    }); 
}); 
} 

這是我的代碼,但我總是得到以下錯誤令牌更新操作由於超時而失敗:空 當我嘗試這樣做純HTML或反應唯一的應用程序,它完美的作品。任何形式的幫助將不勝感激。

回答

0

看看是否加入'catch'並且條件是否有助於識別問題。

function Auth() { 
    return new Promise((resolve, reject) => { 
    const userAgentApplication = new Msal.UserAgentApplication(*my app id*, null, function (errorDes, token, error, tokenType) { 
     // this callback is called after loginRedirect OR acquireTokenRedirect (not used for loginPopup/aquireTokenPopup) 
    }); 

    console.log('inside the promise'); 
    userAgentApplication.loginPopup(["user.read"]) 
    .then((token) => { 
     console.log("Successfully got id token"); 
     console.log("first token: ", token); 
     console.log(userAgentApplication.getUser().name); 

     if (userAgentApplication.getUser()) { 
     userAgentApplication.acquireTokenSilent(["user.read"]) 
     .then((token) => { 
      resolve(token); 
     }) 
     .catch((error) => { 
      reject(error); 
     }); 
     } else { 
     reject("User not logged in"); 
     } 
    }) 
    .catch((error) => { 
     reject(error); 
    }); 
    }); 
} 
相關問題