I want to make an app which using firestore that get a document with reference of their login ID (...document(FirebaseAuth.uid)) after they're logged in but I'm having a problem where the document is null in the first time their login but not after closing and reopening the application. I want to get the document after I'm login, how could I fix them?
Also, when I'm signing out and re-login to my application using another account, my document keeps getting the previous uid. I want to get document with the current uid after sign out, How could fix them?
This is how my app handle login using firebase-auth-ui:
override fun manageFirebaseUIResult(requestCode: Int, resultCode: Int, data: Intent?) {
// RC_SIGN_IN is the request code you passed into startActivityForResult(...) when starting the sign in flow.
if (requestCode == RC_SIGN_IN) {
val response = IdpResponse.fromResultIntent(data)
// Successfully signed in
if (resultCode == Activity.RESULT_OK) {
/* val i = Intent([email protected], BottomNavigation::class.java)
startActivity(i)
*/
onLogin()
return
} else {
// Sign in failed
if (response == null) {
// User pressed back button
// showSnackbar(R.string.sign_in_cancelled);
return
}
if (response.error!!.errorCode == ErrorCodes.NO_NETWORK) {
view.showToast("No Internet Connection")
return
}
if (response.error!!.errorCode == ErrorCodes.UNKNOWN_ERROR) {
// showSnackbar(R.string.unknown_error);
return
}
}
view.showToast("Error")
}
}
Here is my function whenever app is started(using onStart)
override fun onLoadActivity(activity: Activity) {
try {
if (checkGooglePlay(activity)) {
if (auth.currentUser != null) {
Log.d("checkuserisloggedin", "isLoggedIn? ${auth.currentUser}")
Log.d("checkuserisloggedin", "isLoggedIn? ${auth.currentUser?.uid}")
onLogin()
} else {
Log.d("checkuserisloggedin", "isLoggedIn? ${auth.currentUser}")
loadFirebaseUI(activity)
}
}
} catch (e: Throwable) {
view.showToast(message = e.toString())
}
}
Here is my Login function after firebaseAuth succeed logged in
val userRef = FirebaseFirestore.getInstance().collection("user").document(FirebaseAuth.getInstance.uid)
override fun onLogin() {
userRef.get()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val document = task.result
Log.d(TAG, "document = $document")
if (document!!.exists()) {
Log.d(TAG, "document = $document")
Log.d(TAG, "Document is not null${document?.data}")
Log.d(TAG, userId)
view.goToHome()
} else {
Log.e(TAG, "document = $document")
Log.e(TAG, "Document is null ${document?.data} ")
Log.e(TAG, userId)
view.goToRegis()
}
}
}
.addOnFailureListener { e ->
Log.d(TAG,"Error $e")
}
}
And just in case someone ask what is checkGooglePlay:
override fun checkGooglePlay(activity: Activity): Boolean {
val apiAvailability = GoogleApiAvailability.getInstance()
val resultCode = apiAvailability.isGooglePlayServicesAvailable(activity)
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(activity, resultCode, 666).show()
} else {
view.showToast("Google play is not supported in this device")
activity.finish()
}
return false
}
return true
}
It will be printed like this in the first time I logged in:
06-01 19:18:14.247 16987-16987/id.dolife.dolife D/FirebaseInfo: document = DocumentSnapshot{key=users/null, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=null}
06-01 19:18:14.247 16987-16987/id.dolife.dolife D/FirebaseInfo: document = DocumentSnapshot{key=users/null, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=null}
06-01 19:18:14.247 16987-16987/id.dolife.dolife D/FirebaseInfo: Document is null null
06-01 19:18:14.247 16987-16987/id.dolife.dolife D/FirebaseInfo: null
But after I closed the application and reopened it again, it will be like this:
06-01 19:45:48.406 17212-17212/id.dolife.dolife D/FirebaseInfo: document = DocumentSnapshot{key=users/xkeEbB4MA1euO9udv80yyQLEexj1, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=Document{key=users/xkeEbB4MA1euO9udv80yyQLEexj1, data=ArraySortedMap{(intExp=>0.0), (job=>Reader), (joinDate=>Timestamp(seconds=1559286612, nanoseconds=887000000)), (name=>Wh......( version=SnapshotVersion(seconds=1559375078, nanos=845486000), documentState=SYNCED}}
06-01 19:45:48.406 17212-17212/id.dolife.dolife D/FirebaseInfo: document = DocumentSnapshot{key=users/xkeEbB4MA1euO9udv80yyQLEexj1, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=Document{key=users/xkeEbB4MA1euO9udv80yyQLEexj1, data=ArraySortedMap{(intExp=>0......
06-01 19:45:48.407 17212-17212/id.dolife.dolife D/FirebaseInfo: Document is not null{pastStrExp=26.5, past .....