Skip to content

Commit

Permalink
Merge pull request #1672 from Automattic/andy/firebird-issue-1670
Browse files Browse the repository at this point in the history
Go back one screen when confirming a code response says it expired.
  • Loading branch information
notandyvee authored Aug 7, 2024
2 parents abad4fc + f3c21e5 commit dcd5451
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.automattic.simplenote.authentication.magiclink

enum class MagicLinkAuthError(val str: String) {
INVALID_CODE("invalid-code"),
REQUEST_NOT_FOUND("request-not-found"),
UNKNOWN_ERROR("")
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ class MagicLinkConfirmationFragment : Fragment() {
is MagicLinkUiState.Success -> {
completeMagicLinkViewModel.resetState()
hideDialogProgress()
simplenote?.let {
it.loginWithToken(state.email, state.token)
activity?.finish()
}
simplenote.loginWithToken(state.email, state.token)
activity?.finish()
}
is MagicLinkUiState.Error -> {
completeMagicLinkViewModel.resetState()
hideDialogProgress()
Toast.makeText(context, getString(state.messageRes), Toast.LENGTH_LONG).show()
if (state.authError == MagicLinkAuthError.INVALID_CODE && !isRemoving) {
parentFragmentManager.popBackStack()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,33 @@ class OkHttpMagicLinkRepository @Inject constructor(private val simpleHttp: Simp
val user = json.getString("username")
return MagicLinkResponseResult.MagicLinkCompleteSuccess(user, syncToken)
}
Log.d(TAG, "Error completing login: $body")
return MagicLinkResponseResult.MagicLinkError(response.code(), handleErrorMessage(body))
val authError = getErrorFromJson(body)
val errorStringRes = when (authError) {
MagicLinkAuthError.INVALID_CODE -> R.string.magic_link_complete_login_invalid_code_error_message
MagicLinkAuthError.REQUEST_NOT_FOUND -> R.string.magic_link_complete_login_expired_code_error_message
MagicLinkAuthError.UNKNOWN_ERROR -> R.string.magic_link_general_error
}
return MagicLinkResponseResult.MagicLinkError(response.code(), authError, errorStringRes)
}
}

private fun handleErrorMessage(body: String?): Int {
private fun getErrorFromJson(body: String?): MagicLinkAuthError {
try {
val errorJson = JSONObject(body ?: "")
val errorString = errorJson.getString(ERROR_FIELD)
if (errorString == "invalid-code") {
return R.string.magic_link_complete_login_invalid_code_error_message
} else if (errorString == "request-not-found") {
return R.string.magic_link_complete_login_expired_code_error_message
return when(errorString) {
MagicLinkAuthError.INVALID_CODE.str -> {
MagicLinkAuthError.INVALID_CODE
}
MagicLinkAuthError.REQUEST_NOT_FOUND.str -> {
MagicLinkAuthError.REQUEST_NOT_FOUND
}
else -> MagicLinkAuthError.UNKNOWN_ERROR
}
} catch (e: JSONException) {
Log.e(TAG, "Error parsing unsuccessful response", e)
Log.e(TAG, "Error parsing error json", e)
}
return R.string.magic_link_general_error
return MagicLinkAuthError.UNKNOWN_ERROR
}

@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.automattic.simplenote.repositories

import androidx.annotation.StringRes
import com.automattic.simplenote.authentication.magiclink.MagicLinkAuthError

interface MagicLinkRepository {
suspend fun completeLogin(username: String, authCode: String): MagicLinkResponseResult
Expand All @@ -10,5 +11,5 @@ interface MagicLinkRepository {
sealed class MagicLinkResponseResult {
data class MagicLinkCompleteSuccess(val username: String, val syncToken: String) : MagicLinkResponseResult()
data class MagicLinkRequestSuccess(val code: Int) : MagicLinkResponseResult()
data class MagicLinkError(val code: Int, @StringRes val errorMessage: Int? = null) : MagicLinkResponseResult()
data class MagicLinkError(val code: Int, val authError: MagicLinkAuthError = MagicLinkAuthError.UNKNOWN_ERROR, @StringRes val errorMessage: Int? = null) : MagicLinkResponseResult()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.automattic.simplenote.R
import com.automattic.simplenote.authentication.magiclink.MagicLinkAuthError
import com.automattic.simplenote.di.IO_THREAD
import com.automattic.simplenote.repositories.MagicLinkRepository
import com.automattic.simplenote.repositories.MagicLinkResponseResult
Expand Down Expand Up @@ -42,7 +43,7 @@ class CompleteMagicLinkViewModel @Inject constructor(
_magicLinkUiState.postValue(MagicLinkUiState.Success(result.username, result.syncToken))
}
is MagicLinkResponseResult.MagicLinkError -> {
_magicLinkUiState.postValue(MagicLinkUiState.Error(messageRes = result.errorMessage ?: R.string.magic_link_general_error))
_magicLinkUiState.postValue(MagicLinkUiState.Error(result.authError, messageRes = result.errorMessage ?: R.string.magic_link_general_error))
}
else -> _magicLinkUiState.postValue(MagicLinkUiState.Error(messageRes = R.string.magic_link_general_error))
}
Expand All @@ -61,7 +62,7 @@ class CompleteMagicLinkViewModel @Inject constructor(
*/
sealed class MagicLinkUiState {
data class Loading(@StringRes val messageRes: Int): MagicLinkUiState()
data class Error(@StringRes val messageRes: Int): MagicLinkUiState()
data class Error(val authError: MagicLinkAuthError = MagicLinkAuthError.UNKNOWN_ERROR, @StringRes val messageRes: Int): MagicLinkUiState()
data class Success(val email: String, val token: String) : MagicLinkUiState()
object Waiting : MagicLinkUiState()
}

0 comments on commit dcd5451

Please sign in to comment.