@@ -11,9 +11,12 @@ import com.sap.cdc.android.sdk.feature.auth.model.Credentials
1111import com.sap.cdc.android.sdk.feature.auth.session.Session
1212import com.sap.cdc.android.sdk.feature.notifications.IFCMTokenRequest
1313import com.sap.cdc.bitsnbytes.feature.auth.model.AccountEntity
14+ import kotlinx.coroutines.CoroutineScope
15+ import kotlinx.coroutines.Dispatchers
1416import kotlinx.coroutines.flow.MutableStateFlow
1517import kotlinx.coroutines.flow.StateFlow
1618import kotlinx.coroutines.flow.asStateFlow
19+ import kotlinx.serialization.json.Json
1720
1821/* *
1922 * Complete authentication state solution for ViewModels.
@@ -61,6 +64,12 @@ class AuthenticationFlowDelegate(context: Context) {
6164 private val _userAccount = MutableStateFlow <AccountEntity ?>(null )
6265 val userAccount: StateFlow <AccountEntity ?> = _userAccount .asStateFlow()
6366
67+ // JSON serializer for parsing account data
68+ private val json = Json { ignoreUnknownKeys = true }
69+
70+ // Coroutine scope for async operations
71+ private val scope = CoroutineScope (Dispatchers .Main )
72+
6473 init {
6574 // Initialize state based on existing CDC session
6675 syncWithCDCSession()
@@ -111,6 +120,37 @@ class AuthenticationFlowDelegate(context: Context) {
111120 fun refreshAuthenticationState () {
112121 syncWithCDCSession()
113122 }
123+
124+ /* *
125+ * Get account information with state management.
126+ * This method intercepts the response and updates the userAccount StateFlow.
127+ */
128+ suspend fun getAccountInfo (
129+ parameters : MutableMap <String , String >? = mutableMapOf(),
130+ authCallbacks : AuthCallbacks .() -> Unit
131+ ) {
132+ cdc.getAccountInfo(parameters) {
133+ // Register original callbacks first
134+ authCallbacks()
135+
136+ // Add state management side-effect
137+ doOnSuccess { authSuccess ->
138+ try {
139+ // Parse and update the state
140+ val accountData = json.decodeFromString<AccountEntity >(authSuccess.jsonData)
141+ _userAccount .value = accountData
142+ } catch (e: Exception ) {
143+ // Handle parsing errors silently - don't break the callback chain
144+ // Could add logging here if needed
145+ }
146+ }
147+
148+ doOnError { error ->
149+ // Optionally clear account state on error
150+ // _userAccount.value = null
151+ }
152+ }
153+ }
114154}
115155
116156class AuthenticationFlowCDCDelegate (val service : AuthenticationService ) {
@@ -126,11 +166,36 @@ class AuthenticationFlowCDCDelegate(val service: AuthenticationService) {
126166 parameters : MutableMap <String , String > = mutableMapOf()
127167 ) {
128168 service.authenticate().register().credentials(
129- credentials = credentials,
130- configure = authCallbacks,
169+ credentials = credentials, configure = authCallbacks,
131170 parameters = parameters
132171 )
133172 }
173+
174+ suspend fun logOut (authCallbacks : AuthCallbacks .() -> Unit ) {
175+ service.authenticate().logout(authCallbacks = authCallbacks)
176+ }
177+
178+ suspend fun getAccountInfo (
179+ parameters : MutableMap <String , String >? = mutableMapOf(), authCallbacks : AuthCallbacks .() -> Unit
180+ ) {
181+ service.account().get(
182+ parameters = parameters!! ,
183+ configure = authCallbacks
184+ )
185+ }
186+
187+ suspend fun resolvePendingRegistration (
188+ missingFieldsSerialized : MutableMap <String , String >,
189+ regToken : String ,
190+ authCallbacks : AuthCallbacks .() -> Unit
191+ ) {
192+ service.authenticate().register().resolve().pendingRegistrationWith(
193+ missingFields = missingFieldsSerialized,
194+ regToken = regToken,
195+ configure = authCallbacks
196+ )
197+ }
198+
134199}
135200
136201/* *
@@ -139,4 +204,3 @@ class AuthenticationFlowCDCDelegate(val service: AuthenticationService) {
139204fun AuthenticationFlowDelegate.isUserLoggedIn (): Boolean = isAuthenticated.value
140205
141206fun AuthenticationFlowDelegate.getCurrentUser (): AccountEntity ? = userAccount.value
142-
0 commit comments