Skip to content

Auth to cmp issue fix #2

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

Draft
wants to merge 10 commits into
base: auth-fix
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion cmp-navigation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
alias(libs.plugins.mifos.kmp.library)
alias(libs.plugins.mifos.cmp.feature)
alias(libs.plugins.mifos.kmp.koin)
alias(libs.plugins.kotlin.serialization)
}

android {
Expand All @@ -20,6 +21,8 @@ android {
kotlin {
sourceSets {
commonMain.dependencies{
implementation(libs.kotlinx.serialization.json)

implementation(projects.core.domain)
implementation(projects.core.common)
implementation(projects.core.data)
Expand All @@ -29,7 +32,7 @@ kotlin {

// implementation(projects.feature.about)
// implementation(projects.feature.activate)
// implementation(projects.feature.auth)
implementation(projects.feature.auth)
// implementation(projects.feature.center)
// implementation(projects.feature.checkerInboxTask)
// implementation(projects.feature.client)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.mifos.core.datastore.di.PreferencesModule
import com.mifos.core.domain.di.UseCaseModule
import com.mifos.core.network.di.DataManagerModule
import com.mifos.core.network.di.NetworkModule
import com.mifos.feature.auth.di.AuthModule
import com.mifos.room.di.DaoModule
import com.mifos.room.di.HelperModule
import com.mifos.room.di.PlatformSpecificDatabaseModule
Expand Down Expand Up @@ -45,7 +46,7 @@ object KoinModules {
includes(
// AboutModule,
// ActivateModule,
// AuthModule,
AuthModule,
// CenterModule,
// CheckerInboxTaskModule,
// ClientModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import androidx.navigation.compose.composable
import cmp.navigation.App
import cmp.navigation.navigation.NavGraphRoute.MAIN_GRAPH
import com.mifos.core.data.util.NetworkMonitor
import com.mifos.feature.auth.navigation.AuthScreens
import com.mifos.feature.auth.navigation.authNavGraph

@Composable
fun RootNavGraph(
Expand All @@ -26,10 +28,16 @@ fun RootNavGraph(
) {
NavHost(
navController = navHostController,
startDestination = MAIN_GRAPH,
startDestination = AuthScreens.LoginScreenRoute.route,
route = NavGraphRoute.ROOT_GRAPH,
modifier = modifier,
) {
authNavGraph(
navigateHome = { navHostController.navigate(MAIN_GRAPH) },
navigatePasscode = { },
updateServerConfig = {},
)

composable(MAIN_GRAPH) {
App(
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
*/
package com.mifos.room.di

actual val PlatformSpecificDatabaseModule: org.koin.core.annotation.Module
import org.koin.core.module.Module

actual val PlatformSpecificDatabaseModule: Module
get() = TODO("Not yet implemented")
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import kotlinx.coroutines.flow.flow
class LoginUseCase(
private val loginRepository: LoginRepository,
) {

operator fun invoke(
username: String,
password: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package com.mifos.core.network.di

import com.mifos.core.network.BaseApiManager
import com.mifos.core.network.DataManager
import com.mifos.core.network.datamanager.DataManagerAuth
import com.mifos.core.network.datamanager.DataManagerCenter
Expand All @@ -31,7 +32,7 @@ import org.koin.dsl.module

val DataManagerModule = module {
single { DataManager() }
single { DataManagerAuth(get()) }
single { DataManagerAuth(get<BaseApiManager>()) }
single { DataManagerCenter(get(), get(), get(), get()) }
single { DataManagerCharge(get(), get(), get()) }
single { DataManagerCheckerInbox(get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,34 @@
package com.mifos.core.network.di

import coil3.ImageLoader
import com.mifos.core.common.network.MifosDispatchers
import com.mifos.core.common.utils.getInstanceUrl
import com.mifos.core.datastore.UserPreferencesRepository
import com.mifos.core.datastore.UserPreferencesRepositoryImpl
import com.mifos.core.network.BaseApiManager
import com.mifos.core.network.BaseUrl
import com.mifos.core.network.KtorHttpClient
import com.mifos.core.network.KtorfitClient
import com.mifos.core.network.MifosInterceptor
import com.mifos.core.network.utils.ImageLoaderUtils
import de.jensklingenberg.ktorfit.Ktorfit
import io.ktor.client.HttpClient
import io.ktor.client.plugins.auth.Auth
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.koin.core.qualifier.named
import org.koin.dsl.module

val NetworkModule = module {

single<UserPreferencesRepository> {
UserPreferencesRepositoryImpl(
get(),
get(
named(
MifosDispatchers.IO.name,
),
),
get(named(MifosDispatchers.Unconfined)),
)
single<HttpClient>(MifosClient) {
val preferencesRepository = get<UserPreferencesRepository>()

KtorHttpClient.config {
install(Auth)
install(MifosInterceptor) {
repository = preferencesRepository
}
}
}

single<KtorfitClient>(MifosClient) {
Expand All @@ -50,6 +51,13 @@ val NetworkModule = module {

single { BaseApiManager(get(), get()) }

single<Ktorfit> {
Ktorfit.Builder()
.baseUrl(BaseUrl().url)
.httpClient(get<HttpClient>(MifosClient))
.build()
}

single {
val prefManager: UserPreferencesRepository = get()
val baseManager = com.mifos.core.network.apimanager.BaseApiManager.getInstance()
Expand Down
6 changes: 2 additions & 4 deletions feature/auth/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ kotlin {
implementation(compose.material3)
implementation(compose.components.resources)
implementation(compose.ui)
implementation(libs.kermit.simple)
implementation(libs.kermit.logging)
api(projects.core.data)
api(projects.core.domain)
implementation(projects.core.data)
implementation(projects.core.domain)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import com.mifos.core.domain.useCases.PasswordValidationUseCase
import com.mifos.core.domain.useCases.UsernameValidationUseCase
import com.mifos.core.model.objects.users.User
import com.mifos.core.network.model.PostAuthenticationResponse
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -39,7 +37,6 @@ import kotlinx.coroutines.launch

class LoginViewModel(
private val prefManager: UserPreferencesRepository,
// private val context: Context,
private val usernameValidationUseCase: UsernameValidationUseCase,
private val passwordValidationUseCase: PasswordValidationUseCase,
private val loginUseCase: LoginUseCase,
Expand Down Expand Up @@ -71,43 +68,12 @@ class LoginViewModel(
return
}
viewModelScope.launch {
setupPrefManger(username, password)
login(username, password)
}
}

private fun setupPrefManger(username: String, password: String) {
private fun login(username: String, password: String) {
viewModelScope.launch {
_loginUiState.value = LoginUiState.ShowProgress

loginUseCase(username, password).collect { result ->
when (result) {
is DataState.Error -> {
_loginUiState.value =
LoginUiState.ShowError(Res.string.feature_auth_error_login_failed)
Logger.e("Login Error", result.exception)
}

is DataState.Loading -> {
_loginUiState.value = LoginUiState.ShowProgress
}

is DataState.Success -> {
result.data.let { user ->
if (user.userId != null && user.authenticated == true) {
onLoginSuccessful(user, username, password)
} else {
_loginUiState.value =
LoginUiState.ShowError(Res.string.feature_auth_error_login_failed)
}
}
}
}
}
}
}

fun login(username: String, password: String) {
viewModelScope.launch(Dispatchers.IO) {
loginUseCase(username, password).collect { result ->
when (result) {
is DataState.Error -> {
Expand All @@ -121,10 +87,13 @@ class LoginViewModel(
}

is DataState.Success -> {
result.data.let {
if (it.userId != null && it.authenticated == true) {
onLoginSuccessful(it, username, password)
}
if (result.data.authenticated == true) {
onLoginSuccessful(result.data, username, password)
} else {
_loginUiState.value =
LoginUiState.ShowError(Res.string.feature_auth_error_login_failed)

Logger.d("@@@", Throwable("login: ${result.data}"))
}
}
}
Expand Down Expand Up @@ -156,7 +125,7 @@ class LoginViewModel(
if (passcode.value != null) {
_loginUiState.value = LoginUiState.HomeActivityIntent
} else {
_loginUiState.value = LoginUiState.PassCodeActivityIntent
_loginUiState.value = LoginUiState.HomeActivityIntent
}
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ include(":core:ui")

////include(":feature:about")
//include(":feature:activate")
//include(":feature:auth")
include(":feature:auth")
//include(":feature:center")
//include(":feature:checker-inbox-task")
//include(":feature:client")
Expand Down