尝试在 @aws-amplify/auth 6 中获取用户属性时,令牌不是来自此身份池支持的提供商

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

我有一个简单的 React Native 应用程序,它使用 Cognito 进行身份验证,并且在其中一个屏幕中我想显示与登录用户关联的一些用户属性。

我使用“aws-amplify 5.3.x npm 包”完成此工作

import { Auth } from 'aws-amplify'

const user = await Auth.currentAuthenticatedUser()
const { sub, name, email, phone_number } = user.attributes

我尝试迁移到 v6,这导致了错误,因此将其简化为重现相同问题的最简单示例:

import React, { useEffect, useState } from "react";
import { Amplify } from "aws-amplify";
import { Authenticator, useAuthenticator } from "@aws-amplify/ui-react-native";
import {
  FetchUserAttributesOutput,
  fetchUserAttributes,
} from "aws-amplify/auth";
import { Button, Text, View, StyleSheet } from "react-native";

Amplify.configure({
  Auth: {
    Cognito: {
      userPoolId: "eu-west-2_xxx",
      userPoolClientId: "xxx",
      identityPoolId: "eu-west-2:xxx",
      signUpVerificationMethod: "code",
      userAttributes: {
        email: {
          required: true,
        },
        phone_number: {
          required: true,
        },
        name: {
          required: true,
        },
      },
      allowGuestAccess: false,
    },
  },
});

const SignOutButton = () => {
  const { signOut } = useAuthenticator();

  return (
    <View style={styles.signOutButton}>
      <Button title="Sign Out" onPress={signOut} />
    </View>
  );
};

const UserProfile = () => {
  const [attributes, setAttributes] = useState<FetchUserAttributesOutput>();

  useEffect(() => {
    fetchUserAttributes().then(setAttributes).catch(console.error);
  }, []);

  return (
    <View style={styles.container}>
      <Text style={styles.text}>User Profile</Text>
      <Text style={styles.text}>Name: {attributes?.name}</Text>
      <Text style={styles.text}>Phone: {attributes?.phone_number}</Text>
      <SignOutButton />
    </View>
  );
};

const App = () => (
  <Authenticator.Provider>
    <Authenticator>
      <UserProfile />
    </Authenticator>
  </Authenticator.Provider>
);

const styles = StyleSheet.create({
  text: {
    fontSize: 20,
    marginBottom: 20,
  },
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
  signOutButton: {
    alignSelf: "center",
  },
});

export default App;

但是,当我运行此命令并登录时,它会调用

https://cognito-identity.eu-west-2.amazonaws.com/
,它会响应状态代码 400 和错误消息
Token is not from a supported provider of this identity pool.

react-native amazon-cognito aws-amplify
1个回答
0
投票

我没有帮助,但遇到了完全相同的问题

© www.soinside.com 2019 - 2024. All rights reserved.