Skip to content

Commit

Permalink
Delete the token when user fails to log in
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Sep 20, 2023
1 parent 637cc99 commit 5f07c31
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions app/src/main/java/com/infomaniak/drive/ui/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ import com.infomaniak.lib.core.models.ApiError
import com.infomaniak.lib.core.models.ApiResponse
import com.infomaniak.lib.core.models.user.User
import com.infomaniak.lib.core.networking.HttpClient
import com.infomaniak.lib.core.utils.*
import com.infomaniak.lib.core.utils.SnackbarUtils.showSnackbar
import com.infomaniak.lib.core.utils.Utils.lockOrientationForSmallScreens
import com.infomaniak.lib.core.utils.clearStack
import com.infomaniak.lib.core.utils.hideProgress
import com.infomaniak.lib.core.utils.initProgress
import com.infomaniak.lib.core.utils.showProgress
import com.infomaniak.lib.login.ApiToken
import com.infomaniak.lib.login.InfomaniakLogin
import kotlinx.android.synthetic.main.activity_login.*
Expand Down Expand Up @@ -148,30 +145,40 @@ class LoginActivity : AppCompatActivity() {
infomaniakLogin.getToken(
okHttpClient = HttpClient.okHttpClientNoTokenInterceptor,
code = authCode,
onSuccess = {
lifecycleScope.launch(Dispatchers.IO) {
when (val user = authenticateUser(this@LoginActivity, it)) {
is User -> {
trackUserId(AccountUtils.currentUserId)
trackAccountEvent("loggedIn")
launchMainActivity()
}
is ApiResponse<*> -> withContext(Dispatchers.Main) {
if (user.error?.code == ErrorCode.NO_DRIVE) {
launchNoDriveActivity()
} else {
showError(getString(user.translatedError))
}
}
else -> withContext(Dispatchers.Main) { showError(getString(R.string.anErrorHasOccurred)) }
}
}
},
onSuccess = ::onGetTokenSuccess,
onError = { showError(getLoginErrorDescription(this@LoginActivity, it)) },
)
}
}

private fun onGetTokenSuccess(apiToken: ApiToken) {
lifecycleScope.launch(Dispatchers.IO) {
val returnValue = authenticateUser(this@LoginActivity, apiToken)
if (returnValue is User) {
trackUserId(AccountUtils.currentUserId)
trackAccountEvent("loggedIn")
launchMainActivity()
} else {
when (returnValue) {
is ApiResponse<*> -> withContext(Dispatchers.Main) {
if (returnValue.error?.code == ErrorCode.NO_DRIVE) {
launchNoDriveActivity()
} else {
showError(getString(returnValue.translatedError))
}
}
else -> withContext(Dispatchers.Main) { showError(getString(R.string.anErrorHasOccurred)) }
}

infomaniakLogin.deleteToken(
okHttpClient = HttpClient.okHttpClientNoTokenInterceptor,
token = apiToken,
onError = { SentryLog.e("DeleteTokenError", "API response error: $it") },
)
}
}
}

private fun showError(error: String) {
showSnackbar(error)
connectButton?.hideProgress(R.string.connect)
Expand Down

0 comments on commit 5f07c31

Please sign in to comment.