Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: When trying to updateCurrentUserAndDrives(), if we receive a 401, logout the user #1521

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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