我有一个简单的 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.
。
我没有帮助,但遇到了完全相同的问题