mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-13 17:51:57 +00:00
Handle revoked API token and displays success message
- Logs the user out if the API token has been revoked elsewhere (also) - Copy Token - Show success message when password has been changed
This commit is contained in:
parent
2f7df36d98
commit
28fc07d364
3 changed files with 19 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package com.habitrpg.android.habitica.data.implementation
|
package com.habitrpg.android.habitica.data.implementation
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
import com.google.gson.JsonSyntaxException
|
import com.google.gson.JsonSyntaxException
|
||||||
import com.habitrpg.android.habitica.BuildConfig
|
import com.habitrpg.android.habitica.BuildConfig
|
||||||
import com.habitrpg.android.habitica.HabiticaBaseApplication
|
import com.habitrpg.android.habitica.HabiticaBaseApplication
|
||||||
|
|
@ -155,6 +156,14 @@ class ApiClientImpl(
|
||||||
// Modify cache control for 4xx or 5xx range - effectively "do not cache", preventing caching of 4xx and 5xx responses
|
// Modify cache control for 4xx or 5xx range - effectively "do not cache", preventing caching of 4xx and 5xx responses
|
||||||
if (response.code in 400..599) {
|
if (response.code in 400..599) {
|
||||||
when (response.code) {
|
when (response.code) {
|
||||||
|
401 -> {
|
||||||
|
val path = response.request.url.encodedPath
|
||||||
|
if (!path.contains("user/auth/update-password")) {
|
||||||
|
// token has been revoked/rotated elsewhere
|
||||||
|
HabiticaBaseApplication.logout(context)
|
||||||
|
}
|
||||||
|
return@addNetworkInterceptor response
|
||||||
|
}
|
||||||
404 -> {
|
404 -> {
|
||||||
// The server is returning a 404 error, which means the requested resource was not found.
|
// The server is returning a 404 error, which means the requested resource was not found.
|
||||||
// In this case - we want to actually cache the response, and handle it in the app
|
// In this case - we want to actually cache the response, and handle it in the app
|
||||||
|
|
|
||||||
|
|
@ -271,6 +271,10 @@ class AccountPreferenceFragment :
|
||||||
)
|
)
|
||||||
response?.apiToken?.let {
|
response?.apiToken?.let {
|
||||||
viewModel.saveTokens(it, user?.id ?: "")
|
viewModel.saveTokens(it, user?.id ?: "")
|
||||||
|
(activity as? SnackbarActivity)?.showSnackbar(
|
||||||
|
content = getString(R.string.password_changed),
|
||||||
|
displayType = HabiticaSnackbar.SnackbarDisplayType.SUCCESS,
|
||||||
|
)
|
||||||
sheet.dismiss()
|
sheet.dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package com.habitrpg.android.habitica.ui.fragments.preferences
|
package com.habitrpg.android.habitica.ui.fragments.preferences
|
||||||
|
|
||||||
|
import android.content.ClipData
|
||||||
|
import android.content.ClipboardManager
|
||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
|
|
@ -7,6 +9,7 @@ import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.compose.ui.platform.ComposeView
|
import androidx.compose.ui.platform.ComposeView
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.content.ContextCompat.getSystemService
|
||||||
import androidx.core.view.WindowInsetsControllerCompat
|
import androidx.core.view.WindowInsetsControllerCompat
|
||||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||||
import com.habitrpg.android.habitica.R
|
import com.habitrpg.android.habitica.R
|
||||||
|
|
@ -61,6 +64,9 @@ class ApiTokenBottomSheetFragment : BottomSheetDialogFragment() {
|
||||||
content = getString(R.string.copied_to_clipboard, copiedToken),
|
content = getString(R.string.copied_to_clipboard, copiedToken),
|
||||||
displayType = HabiticaSnackbar.SnackbarDisplayType.SUCCESS,
|
displayType = HabiticaSnackbar.SnackbarDisplayType.SUCCESS,
|
||||||
)
|
)
|
||||||
|
val clipboard: ClipboardManager? =
|
||||||
|
context?.let { getSystemService(it, ClipboardManager::class.java) }
|
||||||
|
clipboard?.setPrimaryClip(ClipData.newPlainText("API Token", copiedToken))
|
||||||
dismiss()
|
dismiss()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue