检测用户是否被识别并获取数据

问题描述 投票:0回答:1

我有一个通过 SAML 进行身份验证的网站。 我在这个网站上使用

aws-amplify
库。
我需要知道用户是否已被识别,
如果没有 - 将他导航到 cognito 的地址,该地址将他引导到 SAML 提供商的登录页面。
所以,我使用
aws-amplify
v5 并编写了以下代码:

Auth.currentAuthenticatedUser().then(cognitoUser => {
  ...
}).catch(e => {
  console.log(e);
  window.location.href = this.redirectLink;
});

当用户被识别时 - 他到达了

then
内的部分,
当他身份不明时 - 他到达了
catch
的部分。

现在我正在尝试升级到v6, 我看到 here

currentAuthenticatedUser
函数已被弃用。
本文建议结合使用
getCurrentUser
fetchAuthSession
API。

我尝试同时使用两者和单独使用它们, 但我无法达到检测用户是否已被识别的程度。

getCurrentUser
函数总是落入
catch
并出现错误“UserUnAuthenticatedException:用户需要经过身份验证才能调用此API。”
并且
fetchAuthSession
函数始终返回空属性。

我正在尝试弄清楚,
我是不是做错了什么?
我是不是错过了什么?
还是这个版本真的没有办法知道用户是否被识别?

提前致谢。

angular amazon-cognito aws-amplify saml
1个回答
0
投票

最后,
我是这样做的:

getCurrentUser().then(currentUser => {
  fetchAuthSession().then(cognitoUserSession => {
     this.doSomething(currentUser, cognitoUserSession);
  });
})
.catch(e => {
  signInWithRedirect({
    provider: { custom: "myIdentityProvider" }
  })
});
© www.soinside.com 2019 - 2024. All rights reserved.