英文:
What is the best way to connect a cognito user with a real database user?
问题
I have a react native app build with AWS Amplify. In this, I have a custom authentication flow creating a Cognito user and a post-confirmation Lambda trigger creating a real DynamoDB user. But the problem arises when I want to query this real user in my app's home screen. At present, I do it as follows: I first get the current authenticated user from Cognito, and then from the data received, I query the real database user. Sometimes, at first, this fails to get the user, but when I refresh the screen, I get the user back. I want to know the best way to do this?
My code,
import { Auth } from "aws-amplify";
import { DataStore } from '@aws-amplify/datastore';
import { User } from "../src/models";
const [user, setUser] = useState<User[]>([]);
const [ready, setReady] = useState(false);
const getUser = async () => {
try {
const userData = await Auth.currentAuthenticatedUser();
const currentUserPhone = userData.attributes.phone_number;
if (currentUserPhone) {
await DataStore.query(User, (d) => d.Phonenumb("eq", currentUserPhone)).then(setUser);
console.log("getting user in home");
setReady(true);
} else {
setReady(false);
}
} catch (e) {
setReady(false);
console.log(e);
}
}
useEffect(() => {
getUser();
}, []);
return (
<View>
{ready === true && (<Text>Hello {user?.name}</Text>)}
{ready === false && (<Text>No user</Text>)}
</View>
);
(Note: I've translated the code portion as requested.)
英文:
I have a react native app build with AWS Amplify. In this I have a custom authentication flow creating a cognito user and a post confirmation lambda trigger creating a real dynamoDB user. But the problem arise when I want to query this real user in my app home screen. At present I do it as follows, I first get the current authenticated user from cognito and then from the data receive I query real database user, but sometimes at first this fail to get the user but when I refresh the screen I get the user back. I want to know the best way to do this ?
My code,
import { Auth } from "aws-amplify";
import { DataStore} from '@aws-amplify/datastore';
import { User } from "../src/models";
const [user , setUser] = useState <User[]>([]);
const [ready, setReady] = useState(false);
const getUser = async () => {
try{
const userData = await Auth.currentAuthenticatedUser();
const currentUserPhone = userData.attributes.phone_number
if(currentUserPhone){
await DataStore.query(User, d => d.Phonenumb("eq", currentUserPhone)).then(setUser);
console.log("getting user in home");
setReady(true);
} else{
setReady(false);
}
} catch(e){
setReady(false);
console.log(e)
}
}
useEffect(() => {
getUser();
}, []);
return(
<View>
{ready === true &&(<Text>Hello {user?.name}</Text>)}
{ready === false &&(<Text>No user</Text>)}
</View>
)
答案1
得分: 1
确保在查询之前同步数据。调用 start
并监听 ready
事件。
await DataStore.start();
Hub.listen('datastore', (data) => {
const { event } = data.payload;
if (event === "ready") {
// TODO
}
})
英文:
Ensure data is synced before querying. Call start
and listen for ready
event.
await DataStore.start();
Hub.listen('datastore', (data) => {
const { event } = data.payload;
if (event === "ready") {
// TODO
}
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论