Skip to content

Commit 3f68d4d

Browse files
committed
refactor: error refactoring around AuthError single object
refactor: login/register strict interfacing
1 parent 4a39b33 commit 3f68d4d

31 files changed

+231
-266
lines changed

app/src/main/java/com/sap/cdc/bitsnbytes/feature/auth/AuthenticationFlowDelegate.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import com.sap.cdc.android.sdk.events.emitTokenReceived
1414
import com.sap.cdc.android.sdk.feature.AuthCallbacks
1515
import com.sap.cdc.android.sdk.feature.AuthError
1616
import com.sap.cdc.android.sdk.feature.AuthenticationService
17-
import com.sap.cdc.android.sdk.feature.Credentials
1817
import com.sap.cdc.android.sdk.feature.CustomIdCredentials
18+
import com.sap.cdc.android.sdk.feature.EmailCredentials
1919
import com.sap.cdc.android.sdk.feature.LinkingContext
20+
import com.sap.cdc.android.sdk.feature.LoginIdCredentials
2021
import com.sap.cdc.android.sdk.feature.TwoFactorContext
2122
import com.sap.cdc.android.sdk.feature.biometric.BiometricAuth
2223
import com.sap.cdc.android.sdk.feature.notifications.IFCMTokenRequest
@@ -251,30 +252,29 @@ class AuthenticationFlowDelegate(context: Context) {
251252

252253
//region LOGIN / LOGOUT / REGISTER METHODS
253254

254-
suspend fun login(credentials: Credentials, authCallbacks: AuthCallbacks.() -> Unit) {
255-
authenticationService.authenticate().login()
256-
.credentials(credentials = credentials) {
257-
// Register original callbacks first
258-
authCallbacks()
255+
suspend fun login(credentials: LoginIdCredentials, authCallbacks: AuthCallbacks.() -> Unit) {
256+
authenticationService.authenticate().login().withLoginId(credentials = credentials) {
257+
// Register original callbacks first
258+
authCallbacks()
259259

260-
// Add state management side-effect
261-
doOnSuccess { authSuccess ->
262-
try {
263-
// Parse and update the state
264-
val accountData = json.decodeFromString<AccountEntity>(authSuccess.jsonData)
265-
_userAccount.value = accountData
266-
} catch (e: Exception) {
267-
// Handle parsing errors silently - don't break the callback chain
268-
// Could add logging here if needed
269-
}
260+
// Add state management side-effect
261+
doOnSuccess { authSuccess ->
262+
try {
263+
// Parse and update the state
264+
val accountData = json.decodeFromString<AccountEntity>(authSuccess.jsonData)
265+
_userAccount.value = accountData
266+
} catch (e: Exception) {
267+
// Handle parsing errors silently - don't break the callback chain
268+
// Could add logging here if needed
270269
}
271-
272270
}
271+
272+
}
273273
}
274274

275275
suspend fun loginWithCustomId(credentials: CustomIdCredentials, authCallbacks: AuthCallbacks.() -> Unit) {
276276
authenticationService.authenticate().login()
277-
.customIdentifier(credentials = credentials) {
277+
.withCustomIdentifier(credentials = credentials) {
278278
// Register original callbacks first
279279
authCallbacks()
280280

@@ -313,11 +313,11 @@ class AuthenticationFlowDelegate(context: Context) {
313313
}
314314

315315
suspend fun register(
316-
credentials: Credentials,
316+
credentials: EmailCredentials,
317317
parameters: MutableMap<String, String> = mutableMapOf(),
318318
authCallbacks: AuthCallbacks.() -> Unit,
319319
) {
320-
authenticationService.authenticate().register().credentials(
320+
authenticationService.authenticate().register().emailCredentials(
321321
credentials = credentials, configure = authCallbacks,
322322
parameters = parameters
323323
)

app/src/main/java/com/sap/cdc/bitsnbytes/feature/provider/FacebookAuthenticationProvider.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.facebook.FacebookCallback
66
import com.facebook.FacebookException
77
import com.facebook.login.LoginManager
88
import com.facebook.login.LoginResult
9-
import com.sap.cdc.android.sdk.core.api.model.CDCError
9+
import com.sap.cdc.android.sdk.feature.AuthErrorCodes
1010
import com.sap.cdc.android.sdk.feature.provider.AuthenticatorProviderResult
1111
import com.sap.cdc.android.sdk.feature.provider.IAuthenticationProvider
1212
import com.sap.cdc.android.sdk.feature.provider.ProviderException
@@ -34,7 +34,7 @@ class FacebookAuthenticationProvider : IAuthenticationProvider {
3434
continuation.resumeWithException(
3535
ProviderException(
3636
ProviderExceptionType.HOST_NULL,
37-
CDCError.contextError()
37+
AuthErrorCodes.providerError()
3838
)
3939
)
4040
return@suspendCoroutine
@@ -46,17 +46,16 @@ class FacebookAuthenticationProvider : IAuthenticationProvider {
4646
continuation.resumeWithException(
4747
ProviderException(
4848
ProviderExceptionType.CANCELED,
49-
CDCError.operationCanceled()
49+
AuthErrorCodes.operationCanceled()
5050
)
5151
)
5252
}
5353

5454
override fun onError(error: FacebookException) {
5555
val providerException = ProviderException(
5656
ProviderExceptionType.PROVIDER_FAILURE,
57-
CDCError.providerError()
57+
AuthErrorCodes.providerError().copy(details = error.message)
5858
)
59-
providerException.error?.errorDetails = error.message
6059
continuation.resumeWithException(providerException)
6160
}
6261

@@ -92,4 +91,4 @@ class FacebookAuthenticationProvider : IAuthenticationProvider {
9291
// Facebook SDK unregisters the activityResultRegistry.
9392
}
9493

95-
}
94+
}

app/src/main/java/com/sap/cdc/bitsnbytes/feature/provider/GoogleAuthenticationProvider.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import androidx.credentials.GetCredentialResponse
1010
import androidx.credentials.exceptions.GetCredentialException
1111
import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption
1212
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
13-
import com.sap.cdc.android.sdk.core.api.model.CDCError
13+
import com.sap.cdc.android.sdk.feature.AuthErrorCodes
1414
import com.sap.cdc.android.sdk.feature.provider.AuthenticatorProviderResult
1515
import com.sap.cdc.android.sdk.feature.provider.IAuthenticationProvider
1616
import com.sap.cdc.android.sdk.feature.provider.ProviderException
@@ -32,7 +32,7 @@ class GoogleAuthenticationProvider : IAuthenticationProvider {
3232
if (hostActivity == null) {
3333
val exception = ProviderException(
3434
ProviderExceptionType.HOST_NULL,
35-
CDCError.contextError()
35+
AuthErrorCodes.providerError()
3636
)
3737
throw exception
3838
}
@@ -52,9 +52,8 @@ class GoogleAuthenticationProvider : IAuthenticationProvider {
5252
if (result.failed()) {
5353
val providerException = ProviderException(
5454
ProviderExceptionType.PROVIDER_FAILURE,
55-
CDCError.providerError()
55+
AuthErrorCodes.providerError().copy(details = result.exception?.message.toString())
5656
)
57-
providerException.error?.errorDetails = result.exception?.message.toString()
5857
throw providerException
5958
}
6059

@@ -128,4 +127,4 @@ data class CredentialSignInResult(
128127
) {
129128

130129
fun failed(): Boolean = exception != null
131-
}
130+
}

app/src/main/java/com/sap/cdc/bitsnbytes/feature/provider/LineAuthenticationProvider.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.linecorp.linesdk.Scope
1313
import com.linecorp.linesdk.api.LineApiClientBuilder
1414
import com.linecorp.linesdk.auth.LineAuthenticationParams
1515
import com.linecorp.linesdk.auth.LineLoginApi
16-
import com.sap.cdc.android.sdk.core.api.model.CDCError
16+
import com.sap.cdc.android.sdk.feature.AuthErrorCodes
1717
import com.sap.cdc.android.sdk.feature.provider.AuthenticatorProviderResult
1818
import com.sap.cdc.android.sdk.feature.provider.IAuthenticationProvider
1919
import com.sap.cdc.android.sdk.feature.provider.ProviderException
@@ -48,7 +48,7 @@ class LineAuthenticationProvider() : IAuthenticationProvider {
4848
continuation.resumeWithException(
4949
ProviderException(
5050
ProviderExceptionType.CANCELED,
51-
CDCError.operationCanceled()
51+
AuthErrorCodes.operationCanceled()
5252
)
5353
)
5454
return@suspendCoroutine
@@ -107,7 +107,7 @@ class LineAuthenticationProvider() : IAuthenticationProvider {
107107
continuation.resumeWithException(
108108
ProviderException(
109109
ProviderExceptionType.CANCELED,
110-
CDCError.operationCanceled()
110+
AuthErrorCodes.operationCanceled()
111111
)
112112
)
113113
}
@@ -119,9 +119,8 @@ class LineAuthenticationProvider() : IAuthenticationProvider {
119119
val providerException =
120120
ProviderException(
121121
ProviderExceptionType.PROVIDER_FAILURE,
122-
CDCError.providerError()
122+
AuthErrorCodes.providerError().copy(details = lineResult.errorData.toString())
123123
)
124-
providerException.error?.errorDetails = lineResult.errorData.toString()
125124

126125
dispose()
127126
continuation.resumeWithException(providerException)
@@ -141,4 +140,4 @@ class LineAuthenticationProvider() : IAuthenticationProvider {
141140
override fun dispose() {
142141
launcher?.unregister()
143142
}
144-
}
143+
}

app/src/main/java/com/sap/cdc/bitsnbytes/feature/provider/WeChatAuthenticationProvider.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import android.util.Pair
99
import androidx.activity.ComponentActivity
1010
import androidx.activity.result.ActivityResultLauncher
1111
import androidx.activity.result.contract.ActivityResultContract
12-
import com.sap.cdc.android.sdk.core.api.model.CDCError
12+
import com.sap.cdc.android.sdk.feature.AuthErrorCodes
1313
import com.sap.cdc.android.sdk.feature.provider.AuthenticatorProviderResult
1414
import com.sap.cdc.android.sdk.feature.provider.IAuthenticationProvider
1515
import com.sap.cdc.android.sdk.feature.provider.ProviderException
@@ -43,7 +43,7 @@ class WeChatAuthenticationProvider : IAuthenticationProvider {
4343
continuation.resumeWithException(
4444
ProviderException(
4545
ProviderExceptionType.CANCELED,
46-
CDCError.operationCanceled()
46+
AuthErrorCodes.operationCanceled()
4747
)
4848
)
4949
return@suspendCoroutine
@@ -78,7 +78,7 @@ class WeChatAuthenticationProvider : IAuthenticationProvider {
7878
continuation.resumeWithException(
7979
ProviderException(
8080
ProviderExceptionType.CANCELED,
81-
CDCError.operationCanceled()
81+
AuthErrorCodes.operationCanceled()
8282
)
8383
)
8484
}
@@ -90,7 +90,7 @@ class WeChatAuthenticationProvider : IAuthenticationProvider {
9090
continuation.resumeWithException(
9191
ProviderException(
9292
ProviderExceptionType.CANCELED,
93-
CDCError.operationCanceled()
93+
AuthErrorCodes.operationCanceled()
9494
)
9595
)
9696
}
@@ -102,7 +102,7 @@ class WeChatAuthenticationProvider : IAuthenticationProvider {
102102
continuation.resumeWithException(
103103
ProviderException(
104104
ProviderExceptionType.PROVIDER_FAILURE,
105-
CDCError.providerError()
105+
AuthErrorCodes.providerError()
106106
)
107107
)
108108
}
@@ -125,7 +125,7 @@ class WeChatAuthenticationProvider : IAuthenticationProvider {
125125
continuation.resumeWithException(
126126
ProviderException(
127127
ProviderExceptionType.PROVIDER_FAILURE,
128-
CDCError.providerError()
128+
AuthErrorCodes.providerError()
129129
)
130130
)
131131
}
@@ -149,4 +149,4 @@ class WeChatAuthenticationProvider : IAuthenticationProvider {
149149
override fun dispose() {
150150
launcher?.unregister()
151151
}
152-
}
152+
}

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/EmailRegistrationViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.sap.cdc.bitsnbytes.ui.view.screens
33
import android.content.Context
44
import androidx.lifecycle.viewModelScope
55
import com.sap.cdc.android.sdk.feature.AuthResult
6-
import com.sap.cdc.android.sdk.feature.Credentials
6+
import com.sap.cdc.android.sdk.feature.EmailCredentials
77
import com.sap.cdc.bitsnbytes.extensions.parseRequiredMissingFieldsForRegistration
88
import com.sap.cdc.bitsnbytes.extensions.splitFullName
99
import com.sap.cdc.bitsnbytes.extensions.toJson
@@ -103,7 +103,7 @@ class EmailRegistrationViewModel(
103103
)
104104
)
105105

106-
val credentials = Credentials(
106+
val credentials = EmailCredentials(
107107
email = currentState.email,
108108
password = currentState.password
109109
)

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/EmailSignInViewModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.sap.cdc.bitsnbytes.ui.view.screens
33
import android.content.Context
44
import androidx.lifecycle.viewModelScope
55
import com.sap.cdc.android.sdk.feature.Credentials
6+
import com.sap.cdc.android.sdk.feature.LoginIdCredentials
67
import com.sap.cdc.bitsnbytes.extensions.toJson
78
import com.sap.cdc.bitsnbytes.feature.auth.AuthenticationFlowDelegate
89
import com.sap.cdc.bitsnbytes.ui.state.EmailSignInNavigationEvent
@@ -93,7 +94,7 @@ class EmailSignInViewModel(
9394
_state.update { it.copy(isLoading = true, error = null) }
9495

9596
viewModelScope.launch {
96-
val credentials = Credentials(
97+
val credentials = LoginIdCredentials(
9798
loginId = currentState.email,
9899
password = currentState.password
99100
)

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/OtpVerifyViewModel.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.sap.cdc.bitsnbytes.ui.view.screens
22

33
import android.content.Context
44
import androidx.lifecycle.viewModelScope
5-
import com.sap.cdc.android.sdk.core.api.model.CDCError
5+
import com.sap.cdc.android.sdk.feature.AuthError
66
import com.sap.cdc.android.sdk.feature.ResolvableContext
77
import com.sap.cdc.bitsnbytes.extensions.toJson
88
import com.sap.cdc.bitsnbytes.feature.auth.AuthenticationFlowDelegate
@@ -37,7 +37,7 @@ interface IOtpVerifyViewModel {
3737
code: String,
3838
resolvable: ResolvableContext,
3939
onLogin: () -> Unit,
40-
onFailedWith: (CDCError?) -> Unit
40+
onFailedWith: (AuthError?) -> Unit
4141
) {
4242
//Stub
4343
}
@@ -137,7 +137,7 @@ class OtpVerifyViewModel(
137137
code: String,
138138
resolvable: ResolvableContext,
139139
onLogin: () -> Unit,
140-
onFailedWith: (CDCError?) -> Unit
140+
onFailedWith: (AuthError?) -> Unit
141141
) {
142142
viewModelScope.launch {
143143
// Commented out implementation - keeping stub

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/PendingRegistration.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import androidx.compose.ui.Modifier
2727
import androidx.compose.ui.autofill.ContentType
2828
import androidx.compose.ui.focus.FocusDirection
2929
import androidx.compose.ui.graphics.Color
30-
import androidx.compose.ui.platform.LocalContext
3130
import androidx.compose.ui.platform.LocalFocusManager
3231
import androidx.compose.ui.text.TextStyle
3332
import androidx.compose.ui.text.font.FontWeight
@@ -187,11 +186,12 @@ fun PendingRegistrationView(
187186
fun PendingRegistrationViewPreview() {
188187
PendingRegistrationView(
189188
PendingRegistrationViewModelPreview(),
190-
RegistrationContext(originatingError = AuthError(
191-
message = "Pending registration error",
192-
code = "400",
193-
details = "Missing required fields for registration: nickname",
194-
""
195-
))
189+
RegistrationContext(
190+
originatingError = AuthError(
191+
code = 400,
192+
message = "Pending registration error",
193+
details = "Missing required fields for registration: nickname"
194+
)
195+
)
196196
)
197197
}

0 commit comments

Comments
 (0)