diff options
Diffstat (limited to 'shared')
-rw-r--r-- | shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt index da9b115..3c213d5 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt @@ -85,7 +85,7 @@ class SessionController( } private suspend fun registerFcmToken(token: String) { - print("FCM token: $token\n") + print("Register FCM token: $token\n") try { userFlow.value?.let { user -> val attributes = user.attributes.toMutableMap() @@ -109,9 +109,33 @@ class SessionController( } } - fun logout() { + private suspend fun unregisterFcmToken(token: String) { + print("Unregister FCM token: $token\n") + try { + userFlow.value?.let { user -> + val attributes = user.attributes.toMutableMap() + attributes["notificationTokens"].toString().let { tokens -> + if (tokens.contains("$token,")) { + attributes["notificationTokens"] = + JsonPrimitive(token.replace("$token,", "")) + } else if (tokens.contains(token)) { + attributes["notificationTokens"] = + JsonPrimitive(token.replace(token, "")) + } + usersApi.usersIdPut(user.copy( + attributes = attributes + ), user.id!!) + } + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + fun logout(token: String?) { GlobalScope.launch { loginStateFlow.value = LoginState.Loading + token?.let { unregisterFcmToken(it) } sessionApi.sessionDelete() loginStateFlow.value = LoginState.SignOut } |