Closed Glognus closed 6 months ago
@cwomack do you need more information? Or is there a team dedicated to react native that could take care of this problem? this problem seems to have existed for several months without really having been noticed on react native. Thanks ;)
Hey, @Glognus and thanks for creating this issue. Just wanted to clarify how you're authenticating in your app. Is it just via a basic authentication flow (username/email and password) or is there any additional steps when signing up/signing in with a user that you're doing?
Also, can you provide any sample code snippets for the DataStore API calls that you're making? Thanks!
Also, can you clarify if you're using owner auth as well as what identifier you're using to save/query for the user records? Thanks!
Hi @cwomack , thanks for your reply, i'm using signInWithRedirect
cause i'm using social provider (google, facebook, apple)
Here is a sample for the auth :
Trigger sign in :
const signInWithSocialProvider = async (
provider: SignInWithRedirectInput
) => {
try {
setLoadingAuthSignIn(true);
await signInWithRedirect({
provider: provider.provider,
options: {
preferPrivateSession: true,
},
});
} catch (error) {
console.error("Error signing in with social provider:", error);
} finally {
setLoadingAuthSignIn(false);
}
};
Listen for Auth event
const authListener = Hub.listen("auth", (data) => {
switch (data?.payload?.event) {
case "signedIn":
getAuthenticatedUser();
break;
...
}
});
Call getAuthenticatedUSer to set the current session if not exist :
const getAuthenticatedUser = async () => {
try {
setLoadingUserAuth(true);
const user = await getCurrentUser();
setAuth(user);
await getUserProfile(user); // fetch user profile from Datastore
} catch (error) {
...
} finally {
setLoadingUserAuth(false);
}
};
Fetch user profile from Datastore :
const getUserProfile = async (auth: AuthUser | null) => {
try {
setLoadingProfile(true);
const authUserId = auth?.userId;
if (!authUserId) {
throw new Error(`Auth user id not found`);
}
const users = await DataStore.query(User, (u) =>
u.socialProviderUserName.eq(authUserId)
);
const user = users[0];
if (!user) {
console.log("User profile not found");
return router.navigate("/onboarding");
}
await fetchAllUserProfile(user);
} catch (error) {
...
} finally {
setLoadingProfile(false);
}
};
Here is a sample of my graphql schema :
type User @model @auth(rules: [{ allow: private }, { allow: owner }]) {
id: ID!
name: String!
phone: AWSPhone
email: AWSEmail
hasCompletedOnboarding: Boolean
socialProviderUserName: String
...
}
Please feel free to share your feedback if you believe my approach is appropriate, or if you think a different approach would be better. I sincerely thank you for your assistance.
Hey @Glognus thanks for raising this issue. It seems that you might be running into a race condition where, after clearing the local store and logging in again you are querying for a specific record before it might have synced. I would try delaying the query until the "ready" event has been emitted from the DataStore hub events. Or, you can use observeQuery instead to query for and re-render when the user record has been synced.
Thanks for your feedback, i will take a look
@Glognus - have you had a chance to implement @chrisbonifacio's suggestion above? Thank you!
I have not had the opportunity to reproduce this problem, I prefer to close this issue, thank you anyway !
Before opening, please confirm:
JavaScript Framework
React Native
Amplify APIs
Authentication, DataStore
Amplify Version
v6
Amplify Categories
auth, api
Backend
Amplify CLI
Environment information
Describe the bug
I'm developing a React Native application that handles user authentication using AWS Cognito. After a user logs in via Cognito, I use
DataStore.query
to fetch their information from AWS Amplify DataStore. If no information is found, it indicates the user is new, and we proceed to onboarding. Otherwise, the existing information is retrieved and displayed.This process works correctly on the first login. However, after logging out and attempting to log in with a different user account, using
DataStore.clear()
to clear local data, an issue occurs. When I executeDataStore.query
to fetch the new user's information, the query returnsnull
on the first attempt, even though the user's data exists in the database.Expected behavior
Expected behavior The new user's information should be correctly fetched after executing DataStore.query following re-login and the DataStore.clear() call.
Current Behavior The first attempt to fetch the new user's information via DataStore.query returns null, despite the user's data being present in the database.
Reproduction steps
Code Snippet
No response
Log output
No response
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response