将一个Cognito用户与一个真实的数据库用户连接的最佳方式是什么?

huangapple go评论59阅读模式
英文:

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 &quot;aws-amplify&quot;;
import { DataStore} from &#39;@aws-amplify/datastore&#39;;
import { User } from &quot;../src/models&quot;;

const [user , setUser] = useState &lt;User[]&gt;([]);
const [ready, setReady] = useState(false);

const getUser = async () =&gt; {

try{
    const userData = await Auth.currentAuthenticatedUser();
    const currentUserPhone = userData.attributes.phone_number
    
    if(currentUserPhone){  
     await DataStore.query(User, d =&gt; d.Phonenumb(&quot;eq&quot;, currentUserPhone)).then(setUser);
     console.log(&quot;getting user in home&quot;);
     setReady(true);
    } else{
     setReady(false);
    }
  } catch(e){
    setReady(false);
    console.log(e)
  }

}

useEffect(() =&gt; {
    
  getUser();
     
}, []);

return(

&lt;View&gt;
 {ready === true &amp;&amp;(&lt;Text&gt;Hello {user?.name}&lt;/Text&gt;)}
 {ready === false &amp;&amp;(&lt;Text&gt;No user&lt;/Text&gt;)}
&lt;/View&gt;

)

答案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(&#39;datastore&#39;, (data) =&gt; {
  const  { event } = data.payload;
  if (event === &quot;ready&quot;) {
    // TODO
  }
})

huangapple
  • 本文由 发表于 2023年2月6日 21:19:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定