Skip to content

Commit

Permalink
update error auth toke when init app
Browse files Browse the repository at this point in the history
  • Loading branch information
Hcnc100 committed Oct 1, 2022
1 parent 81d30f0 commit a035cc9
Showing 1 changed file with 44 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,56 @@ class AuthDataSourceImpl : AuthDataSource {


override suspend fun addingTokenUser(newToken: String?, uuidUser: String?, oldToken: String) {

val idDocument = uuidUser ?: auth.currentUser!!.uid
val finishToken = newToken ?: Firebase.messaging.token.await()

val refToken = refCollectionTokens.document(idDocument)

database.runTransaction { transaction ->
val currentToken = transaction.get(refToken)
// && (currentToken.get(FIELD_ARRAY_TOKENS) as? List<*>) == null
if (currentToken.exists()) {

(currentToken.get(FIELD_ARRAY_TOKENS) as? HashMap<*, *>)?.let {
if (!it.containsKey(finishToken)) {
transaction.update(
/* documentRef = */
refToken,
/* field = */
"$FIELD_ARRAY_TOKENS.$finishToken",
/* value = */
mapOf(FIELD_CREATE_USER to FieldValue.serverTimestamp()),
)
}
(uuidUser ?: auth.currentUser?.uid)?.let { idDocument ->
val refToken = refCollectionTokens.document(idDocument)

if (it.containsKey(oldToken)) {
transaction.update(
/* documentRef = */
refToken,
/* field = */
"$FIELD_ARRAY_TOKENS.$oldToken",
/* value = */
FieldValue.delete(),
)
database.runTransaction { transaction ->
val currentToken = transaction.get(refToken)
// && (currentToken.get(FIELD_ARRAY_TOKENS) as? List<*>) == null
if (currentToken.exists()) {

(currentToken.get(FIELD_ARRAY_TOKENS) as? HashMap<*, *>)?.let {
if (!it.containsKey(finishToken)) {
transaction.update(
/* documentRef = */
refToken,
/* field = */
"$FIELD_ARRAY_TOKENS.$finishToken",
/* value = */
mapOf(FIELD_CREATE_USER to FieldValue.serverTimestamp()),
)
}

if (it.containsKey(oldToken)) {
transaction.update(
/* documentRef = */
refToken,
/* field = */
"$FIELD_ARRAY_TOKENS.$oldToken",
/* value = */
FieldValue.delete(),
)
}
}

} else {
transaction.set(
/* documentRef = */
refToken,
/* data = */
hashMapOf(
FIELD_ARRAY_TOKENS to mapOf(
finishToken to mapOf(FIELD_CREATE_USER to FieldValue.serverTimestamp())
)
),
)
}
}.await()
}


} else {
transaction.set(
/* documentRef = */
refToken,
/* data = */
hashMapOf(
FIELD_ARRAY_TOKENS to mapOf(
finishToken to mapOf(FIELD_CREATE_USER to FieldValue.serverTimestamp())
)
),
)
}
}.await()
}

override suspend fun authWithEmailAndPassword(email: String, pass: String): UserAuth {
Expand Down

0 comments on commit a035cc9

Please sign in to comment.