Created
March 28, 2024 12:40
-
-
Save eoguvo/b9e6ab85af3f9634cac7a8abb7e3a9a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const userSnapshot = useRef<Unsubscribe|null>(null); | |
const [isFirstUser, setIsFirstUser] = useState(false); | |
const [firstUserFinded, setFirstUserFinded] = useState(false); | |
useEffect(function () { | |
if (userSnapshot.current && firstUserFinded) { | |
userSnapshot.current(); | |
userSnapshot.current = null; | |
} | |
}, [firstUserFinded]); | |
useEffect(function () { | |
if (firstUserFinded) { | |
return; | |
} | |
if (!isFirstUser || !userId) { | |
return; | |
} | |
const documentReference = doc(Firestore, "users", userId); | |
userSnapshot.current = onSnapshot(documentReference, async function (snapshot) { | |
if (!snapshot.exists()) { | |
return; | |
} | |
try { | |
const user = snapshot.data() as TypeUser; | |
const serializedUser = { ...user, userId: user.userId || userId }; | |
const userPrivate = await FirestoreUsersPrivateFind({ docId: userId }); | |
await setSessionLogIn(serializedUser, userPrivate); | |
await getPermissions(); | |
setFirstUserFinded(true); | |
createToast("Sucesso!", "Autenticado com sucesso", "SUCCESS"); | |
return; | |
} | |
catch (error) { | |
createToast("Oops!", Translation.toast.text.something_went_wrong_try_again, "WARNING"); | |
} | |
} as (snapshot: DocumentSnapshot<DocumentData, DocumentData>) => void); | |
}, [firstUserFinded, isFirstUser, Translation, userId, setSessionLogIn, createToast, getPermissions]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment