1

我有一个使用 AWS Amplify 构建的反应应用程序。我按照入门指南并在 App.js 中初始化项目,如下所示:

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

在我的 NavbarComponent 中,我从“aws-amplify”导入 Auth,并且我的注册/登录按钮上有一个 onClick 处理程序,它只调用 Auth.federatedSignIn:

<Menu.Item>
  <Link to="/" onClick={ Auth.federatedSignIn }>Login/Register</Link>
</Menu.Item>

当我单击此按钮时,我收到以下错误:

Unhandled Rejection (TypeError): Cannot read property '_config' of undefined

以及以下代码片段:

(anonymous function)
src/Auth.ts:1713
  1710 | dispatchAuthEvent('cognitoHostedUI', currentUser, "A user " + currentUser.getUsername() + " has been signed in via Cognito Hosted UI");
  1711 | if (isCustomStateIncluded) {
  1712 |     customState = state
> 1713 |         .split('-')
       | ^  1714 |         .splice(1)
  1715 |         .join('-');
  1716 |     dispatchAuthEvent('customOAuthState', customState, "State for user " + currentUser.getUsername());

当我使用运行“放大状态”后提供的托管 UI 测试链接时,登录效果很好。我使用身份提供者之一登录,我被重定向到主页就好了。只有在尝试使用我的代码中的 Auth.federatedSignIn 函数登录或注册时,我才会遇到这个问题。

我已经尝试再次拉下放大状态以确保 aws-exports.js 文件已更新,但这并没有奏效。我还尝试使用 Amplify.configure() 并直接在我正在调用 Auth.federatedSignIn 的 NavbarComponent 中传入导入的 aws-exports.js,但这并没有改变任何东西。

非常感谢任何帮助解决这个问题!

此外,为了更好地衡量,这里是我的 aws-exports.js 文件的净化版本:

// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile = {
    "aws_project_region": "<region>",
    "aws_content_delivery_bucket": "<app-name>",
    "aws_content_delivery_bucket_region": "<region>",
    "aws_content_delivery_url": "http://<base-url>.amazonaws.com",
    "aws_appsync_graphqlEndpoint": "https://<base-url>.amazonaws.com/graphql",
    "aws_appsync_region": "<region>",
    "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
    "aws_cognito_identity_pool_id": "<cognito-identity-pool-id>",
    "aws_cognito_region": "<region>",
    "aws_user_pools_id": "<aws-user-pool-id>",
    "aws_user_pools_web_client_id": "<aws-user-pools-web-client-id>",
    "oauth": {
        "domain": "<my-app-name>.auth.<region>.amazoncognito.com",
        "scope": [
            "phone",
            "email",
            "openid",
            "profile",
            "aws.cognito.signin.user.admin"
        ],
        "redirectSignIn": "http://localhost:3000/",
        "redirectSignOut": "http://localhost:3000/",
        "responseType": "code"
    },
    "federationTarget": "COGNITO_USER_AND_IDENTITY_POOLS"
};


export default awsmobile;
4

1 回答 1

0

federatedSignIn函数接受一个对象。

import { CognitoHostedUIIdentityProvider } from "@aws-amplify/auth/lib/types";

...

const user = await Auth.federatedSignIn({
  provider: CognitoHostedUIIdentityProvider.Google
});
于 2021-05-30T19:17:07.900 回答