reactjs AuthError -错误:未正确配置放大

8oomwypt  于 2023-05-06  发布在  React
关注(0)|答案(9)|浏览(180)

首先,我已经成功地使用amplify configure完成了对react应用程序的配置。我在AWS Amplify docs的帮助下做到了这一点。然后,我成功地使用amplify add authamplify push将身份验证添加到我的amplify项目中。我遵循了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时,它显示以下错误:

dfty9e19

dfty9e191#

我在这个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);
oknwwptz

oknwwptz2#

  • npm un aws-amplify @aws-amplify/ui-react
  • npm i aws-amplify @aws-amplify/ui-react

这对我很有效。谢谢@伊格纳西奥

mrfwxfqh

mrfwxfqh3#

我认为这个问题发生在不同的放大模块版本,由于安装的放大模块之间的不一致。在我的情况下,重新安装如下解决了很多次。
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

yebdmbv4

yebdmbv44#

我在世博会上做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'
njthzxwz

njthzxwz5#

如果你使用的是Yarn,这个问题可能是由包管理器冲突引起的,基于它们如何管理依赖关系树和版本更新。
如果你反复看到这个问题;在某些情况下,您应该尝试使用Npm。
如果你正在使用Yarn -你应该首先删除Yarn.lock和你的node_modules目录。npm install
另外,请参见上面的答案Untamables Answer

fxnxkyjh

fxnxkyjh6#

运行放大更新授权
选择Walkthrough所有授权配置。
在演练中启用未经身份验证的登录,并保留其他设置。
来源:https://docs.amplify.aws/lib/graphqlapi/authz/q/platform/js/#using-amplify-graphql-client
使用AWS_IAM进行公共API访问时,必须启用未经身份验证的登录。要启用未经身份验证的登录,请从命令行运行amplify update auth,然后选择Walkthrough all the auth configurations。
这解决了我的问题,结合graphQL API

hfwmuf9z

hfwmuf9z7#

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

kgqe7b3p

kgqe7b3p8#

If you are following a tutorial here are some precautions that might be helpful to you and save you same headache.
Use modules and dependencies that the tutor of the tutorial is using. Do not assume anything.

in package.json file write the dependencies and version as guided by the tutorial and at the end dont run npm install.
why? npm install pops up ERR errors that are headaches. Use yarn install and it will fix your errors.

e.g
in my project's package.json file i have the following::
{

"version": "0.1.0",
  "private": true,
  "dependencies": {
    "@aws-amplify/ui-react": "^0.2.9-unstable.12",
    "@stripe/react-stripe-js": "^1.9.0",
    "@stripe/stripe-js": "^1.32.0",
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "aws-amplify": "^4.3.26",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-icons": "^3.8.0",
    "react-router-dom": "^6.3.0",
    "react-scripts": "5.0.1",
    "react-stripe-elements": "^6.0.1",
    "stripe": "^9.10.0",
    "uuid": "^7.0.0",
    "web-vitals": "^2.1.4"
  },
}

from the guide of the tutorial.But from react all these dependencies are old some obsolete. What do i do? I dont install each individual to bring confusion in modules by just running yarn install to align them and erradicet errors.
trnvg8h3

trnvg8h39#

此错误可能是由于Amplify CLI配置错误而出现的。对我来说就是这样。
运行amplify configure并按照说明操作。

相关问题