2

我正在尝试使用 REDUX 中的 MSAL 库进行身份验证,但遇到了一些麻烦。当我制作一个仅反应的应用程序并做同样的事情时,我成功获得了访问令牌,但尝试在 REDUX 中使用它,我在尝试获取访问令牌时总是超时。

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);
    });
});
}

这是我拥有的代码,但我总是收到以下错误令牌更新操作因超时而失败:null 当我尝试在纯 HTML 中执行此操作或仅响应应用程序时,它可以完美运行。任何形式的帮助将不胜感激。

4

1 回答 1

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);
    });
  });
}
于 2017-07-20T18:04:57.797 回答