auth broken

This commit is contained in:
2025-12-03 17:34:25 +03:00
parent 257755a25a
commit 14cf326e0b
5 changed files with 38 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
package ru.myitschool.work.core
object Constants {
const val HOST = "http://192.168.1.39:8080"
const val HOST = "http://172.22.21.143:8080"
const val AUTH_URL = "/auth"
const val INFO_URL = "/info"
const val BOOKING_URL = "/booking"

View File

@@ -1,21 +1,34 @@
package ru.myitschool.work.data.repo
import android.content.Context
import ru.myitschool.work.data.source.DataStoreDataSource.createAuthCode
import ru.myitschool.work.data.source.NetworkDataSource
object AuthRepository {
private var codeCache: String? = null
suspend fun checkAndSave(text: String): Result<Boolean> {
/* return NetworkDataSource.checkAuth(text).onSuccess { success ->
if (success) {
codeCache = text
createAuthCode(code = text)
return try {
val result = NetworkDataSource.checkAuth(text)
when {
result.isSuccess && result.getOrNull() == true -> {
codeCache = text
createAuthCode(code = text)
Result.success(true)
}
result.isFailure -> {
val exception = result.exceptionOrNull()
val errorMessage = exception?.message ?: "Ошибка авторизации"
Result.failure(Exception(errorMessage))
}
else -> {
Result.success(false)
}
}
} catch (e: Exception) {
Result.failure(e)
}
} */
codeCache = text
createAuthCode(code = text)
return Result.success(true) // TODO: ВЕРНУТЬ СЕТЕВОЙ ЗАПРОС
}
}

View File

@@ -35,10 +35,12 @@ object NetworkDataSource {
suspend fun checkAuth(code: String): Result<Boolean> = withContext(Dispatchers.IO) {
return@withContext runCatching {
val response = client.get(getUrl(code, Constants.AUTH_URL)) // TODO: Отпрвка запроса на сервер
val response = client.get(getUrl(code, Constants.AUTH_URL))
when (response.status) {
HttpStatusCode.OK -> true
else -> error(response.bodyAsText())
HttpStatusCode.Unauthorized, HttpStatusCode.BadRequest -> false
else -> error("Request error: ${response.bodyAsText()}")
}
}
}

View File

@@ -2,14 +2,14 @@ package ru.myitschool.work.domain.auth
import ru.myitschool.work.data.repo.AuthRepository
class CheckAndSaveAuthCodeUseCase(
private val repository: AuthRepository
) {
suspend operator fun invoke(
text: String
): Result<Unit> {
return repository.checkAndSave(text).mapCatching { success ->
if (!success) error("Code is incorrect")
class CheckAndSaveAuthCodeUseCase(
private val repository: AuthRepository
) {
suspend operator fun invoke(
text: String
): Result<Unit> {
return repository.checkAndSave(text).mapCatching { success ->
if (!success) error("Code is incorrect")
}
}
}
}
}

View File

@@ -38,6 +38,7 @@ class AuthViewModel : ViewModel() {
},
onFailure = { error ->
error.printStackTrace()
error.message?.let { Log.d("AnnaKonda", it) }
if (error.message != null) {
_actionFlow.emit(AuthAction.ShowError(error.message.toString()))
}