reactjs - 如何响应 @aws-amplify/ui-react 中身份验证状态的变化

标签 reactjs authentication aws-amplify

使用较旧的 aws-amplify-react,为了在成功登录后将用户重定向到请求的 URL,我有:

<Authenticator
  onStateChange={authState => {
    if (authState === 'signedIn') {
      const { from } = this.props.location.state || {
        from: {
          pathname: '/',
          search: ''
        }
      };
      this.props.history.replace(from.pathname + from.search);
    }
  }}
/>

来自documentation对于新版本 (@aws-amplify/ui-react),我了解如何 access the auth state使用 useAuthenticator Hook ,但没有内置工具来处理此状态下的更改。是否不受支持,我现在需要实现自己的更改检测?

最佳答案

没有任何改变,钩子(Hook)useAuthenticator可以返回表示当前authStateroute:

const { route } = useAuthenticator(context => [context.route]);

if (route === "authenticated"){
   
    const location = useLocation();
    const history = useHistory();

    const { from } = location.state || { from: { pathname: '/', search: '' } }; 
    history.replace(from.pathname + from.search);
}

关于reactjs - 如何响应 @aws-amplify/ui-react 中身份验证状态的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71431539/

相关文章:

javascript - 如何在刷新浏览器时将应用程序重定向到主页?

php - Laravel 重定向与注销不起作用

sql-server - 如何让window后台服务访问SQL Server数据库?

javascript - 使用 Cognito : How do I get the Current User's Email? 放大

javascript - 如何直接打开 Amazon Cognito 托管 UI 的注册页面?

javascript - 用 React 循环?

javascript - 递归函数如何将消息保存在数组中?

javascript - 具有动态样式的 ReactCSSTransitionsGroup 无法在第一项以外的任何内容上正确执行输入动画

authentication - 企业代理背后的 asp.net core 3.1 azure ad SSO 身份验证

amazon-web-services - 如何在 appsync 和 amplify 项目中使用 schema.graphql 而不是 schema.json?