使用带有REDUX的MSAL(Microsoft身份验证库)

时间:2017-07-20 00:15:54

标签: reactjs redux msal

我正在尝试使用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中执行此操作或仅响应应用程序时,它可以完美地运行。任何形式的帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分: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);
    });
  });
}
相关问题