aboutsummaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-01-15 01:32:56 -0600
committerIván Ávalos <avalos@disroot.org>2022-01-15 01:32:56 -0600
commita646440e5384c5adb9914211666fcc776cf87567 (patch)
tree2d21f7a4a2da7fc168ba71cf6818952e9336ebd5 /shared
parenta48d8624d2ee876eac1fed2d0c32af7dccb4a195 (diff)
downloadetbsa-trackermap-mobile-a646440e5384c5adb9914211666fcc776cf87567.tar.gz
etbsa-trackermap-mobile-a646440e5384c5adb9914211666fcc776cf87567.tar.bz2
etbsa-trackermap-mobile-a646440e5384c5adb9914211666fcc776cf87567.zip
FCM token unregisters when logging out
Diffstat (limited to 'shared')
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt28
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
}