Skip to content

Commit

Permalink
fix: When trying to updateCurrentUserAndDrives(), if we receive a `…
Browse files Browse the repository at this point in the history
…401`, logout the user
  • Loading branch information
KevinBoulongne committed Feb 11, 2025
1 parent 6ac1bdb commit 27e75ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object ErrorCode {
const val PASSWORD_NOT_VALID = "password_not_valid"
const val PUBLIC_SHARE_LINK_IS_NOT_VALID = "link_is_not_valid"
const val QUOTA_EXCEEDED_ERROR = "quota_exceeded_error"
const val NOT_AUTHORIZED = "not_authorized"

val apiErrorCodes = listOf(
ApiErrorCode(CATEGORY_ALREADY_EXISTS, R.string.errorCategoryAlreadyExists),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import androidx.navigation.fragment.FragmentNavigatorExtras
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.infomaniak.drive.R
import com.infomaniak.drive.data.cache.DriveInfosController
import com.infomaniak.drive.data.models.UploadFile
import com.infomaniak.drive.databinding.FragmentMenuBinding
import com.infomaniak.drive.ui.MainViewModel
import com.infomaniak.drive.utils.AccountUtils
Expand Down Expand Up @@ -110,8 +109,7 @@ class MenuFragment : Fragment() {
.setMessage(getString(R.string.alertRemoveUserDescription, currentUser.displayName))
.setPositiveButton(R.string.buttonConfirm) { _, _ ->
lifecycleScope.launch(Dispatchers.IO) {
if (UploadFile.getAppSyncSettings()?.userId == currentUser.id) UploadFile.deleteAllSyncFile()
AccountUtils.removeUserAndDeleteToken(requireContext(), currentUser)
AccountUtils.deleteSyncFilesThenLogoutUser(requireContext(), currentUser)
}
}
.setNegativeButton(R.string.buttonCancel) { _, _ -> }
Expand Down
30 changes: 21 additions & 9 deletions app/src/main/java/com/infomaniak/drive/utils/AccountUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,33 @@ object AccountUtils : CredentialManager() {
okHttpClient: OkHttpClient = HttpClient.okHttpClient,
) = withContext(Dispatchers.IO) {

val (userResult, user) = with(ApiRepository.getUserProfile(okHttpClient)) {
result to (data ?: return@withContext)
}
val apiResponse = ApiRepository.getUserProfile(okHttpClient)

if (userResult != ApiResponseStatus.ERROR) {
ApiRepository.getAllDrivesData(okHttpClient).apply {
if (result != ApiResponseStatus.ERROR) {
handleDrivesData(context, fromMaintenance, fromCloudStorage, user, data as DriveInfo)
} else if (error?.code == ErrorCode.NO_DRIVE) {
removeUserAndDeleteToken(context, user)
if (apiResponse.result == ApiResponseStatus.ERROR) {
when (apiResponse.error?.code) {
ErrorCode.NOT_AUTHORIZED -> {
val user = currentUser ?: requestCurrentUser() ?: return@withContext
deleteSyncFilesThenLogoutUser(context, user)
}
}
return@withContext
}

val user = apiResponse.data ?: return@withContext
ApiRepository.getAllDrivesData(okHttpClient).apply {
if (result != ApiResponseStatus.ERROR) {
handleDrivesData(context, fromMaintenance, fromCloudStorage, user, data as DriveInfo)
} else if (error?.code == ErrorCode.NO_DRIVE) {
removeUserAndDeleteToken(context, user)
}
}
}

suspend fun deleteSyncFilesThenLogoutUser(context: Context, user: User) {
if (UploadFile.getAppSyncSettings()?.userId == user.id) UploadFile.deleteAllSyncFile()
removeUserAndDeleteToken(context, user)
}

private suspend fun handleDrivesData(
context: Context,
fromMaintenance: Boolean,
Expand Down

0 comments on commit 27e75ab

Please sign in to comment.