【问题标题】:AuthError - Error: Amplify has not been configured correctlyAuthError - 错误:未正确配置 Amplify
【发布时间】:2020-12-15 17:38:01
【问题描述】:

首先,我使用amplify configure 成功完成了我的react 应用程序的配置。我在AWS Amplify docs 的帮助下做到了这一点。然后,我使用amplify add authamplify push 成功为我的放大项目添加了身份验证。我按照AWS - Authentication with Amplify Doc中的所有步骤进行操作

我的App.js 看起来像这样,

import React from 'react';
import { withAuthenticator, AmplifySignOut } from '@aws-amplify/ui-react';
import Amplify, { Auth } from 'aws-amplify';
import awsconfig from './aws-exports';

Amplify.configure(awsconfig);


const App = () => (
    <div>
        <AmplifySignOut />
        My App
    </div>
);

export default withAuthenticator(App);

但是当我尝试npm start 时,它显示以下错误,

【问题讨论】:

    标签: reactjs amazon-web-services aws-amplify


    【解决方案1】:

    我在github-issue找到了解决这个问题的方法

    修复很简单。 Amplify 文档不会告诉您将 aws-exports 的配置加载到 Auth module

    App.js 中添加这行简单的代码,为我解决了这个问题。

    import Amplify, { Auth } from 'aws-amplify';
    import awsconfig from './aws-exports';
    
    Amplify.configure(awsconfig);
    
    // >>New - Configuring Auth Module
    Auth.configure(awsconfig);
    

    【讨论】:

    • 太棒了!这也为我解决了这个问题。这可能是更新版本的 amplify 中的新功能,因为我之前放大身份验证工作完美无缺,升级 aws-amplify 后出现问题。
    • 花了几个小时来识别它。这个效果很棒。
    • 就我而言,我必须重新安装依赖项才能使其正常工作 (npm un aws-amplify @aws-amplify/ui-react / npm i aws-amplify @aws-amplify/ui-react)
    • awsconfig 等于什么?
    【解决方案2】:

    我认为由于安装的 Amplify 模块之间的不一致,在各种 Amplify 模块版本下都会出现此问题。 在我的情况下,重新安装如下解决了很多次。

    npm uninstall --save aws-amplify @aws-amplify/ui-react @aws-amplify/ui-components

    npm install --save aws-amplify @aws-amplify/ui-react @aws-amplify/ui-components

    有一种情况需要重新安装@aws-amplify/ui-components如果你使用的话。

    【讨论】:

    • 这对我有用。无需导入“Auth”模块和/或执行 Auth.configure()
    【解决方案3】:
    • npm un aws-amplify @aws-amplify/ui-react
    • npm i aws-amplify @aws-amplify/ui-react

    这对我有用。谢谢@Ignacio

    【讨论】:

    • 这对我有用。我有“@aws-amplify/ui-react”:“^0.2.24”和“aws-amplify”:“^3.3.4”。
    【解决方案4】:

    如果您使用 Yarn,则此问题可能是由于包管理器根据它们管理依赖关系树和版本更新的方式而发生冲突。

    如果您反复看到此问题;在某些情况下,您应该尝试使用 Npm。

    如果你使用 Yarn - 你应该首先删除 Yarn.lock 和你的 node_modules 目录。 npm 安装

    另外,也请看上面的答案Untamables Answer

    【讨论】:

      【解决方案5】:

      运行放大更新验证

      选择演练所有身份验证配置。

      在演练中启用未经身份验证的登录并保留其他设置。

      来源:https://docs.amplify.aws/lib/graphqlapi/authz/q/platform/js/#using-amplify-graphql-client

      使用 AWS_IAM 进行公共 API 访问时,必须启用未经身份验证的登录。要启用未经身份验证的登录,请从命令行运行 amplify update auth 并选择 Walkthrough all auth 配置。

      结合 graphQL API 解决了我的问题

      【讨论】:

        【解决方案6】:

        我现在正在处理这个错误,但没有安装 @aws-amplify/ui-react。我相信对 Auth 从版本 3 到 4 进行了更改,这会导致问题

        【讨论】:

          【解决方案7】:

          我在 Expo 中做 todo 应用,遇到了同样的问题。 我必须为配置文件添加正确的路径。 aws-exports 的路径不同,文档中未提及。 我的示例代码如下

          import awsconfig from './src/aws-exports'
          
          Amplify.configure(awsconfig);
          Auth.configure(awsconfig);
          
          import { createTodo } from './src/graphql/mutations'
          import { listTodos } from './src/graphql/queries'
          import { withAuthenticator } from 'aws-amplify-react-native'
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-12-16
            • 2021-11-20
            • 2021-01-30
            • 1970-01-01
            • 2023-03-22
            • 2020-01-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多