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 package ru.myitschool.work.core
object Constants { 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 AUTH_URL = "/auth"
const val INFO_URL = "/info" const val INFO_URL = "/info"
const val BOOKING_URL = "/booking" const val BOOKING_URL = "/booking"

View File

@@ -1,21 +1,34 @@
package ru.myitschool.work.data.repo package ru.myitschool.work.data.repo
import android.content.Context
import ru.myitschool.work.data.source.DataStoreDataSource.createAuthCode import ru.myitschool.work.data.source.DataStoreDataSource.createAuthCode
import ru.myitschool.work.data.source.NetworkDataSource import ru.myitschool.work.data.source.NetworkDataSource
object AuthRepository { object AuthRepository {
private var codeCache: String? = null private var codeCache: String? = null
suspend fun checkAndSave(text: String): Result<Boolean> { suspend fun checkAndSave(text: String): Result<Boolean> {
/* return NetworkDataSource.checkAuth(text).onSuccess { success -> return try {
if (success) { val result = NetworkDataSource.checkAuth(text)
when {
result.isSuccess && result.getOrNull() == true -> {
codeCache = text codeCache = text
createAuthCode(code = 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) {
codeCache = text Result.failure(e)
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) { suspend fun checkAuth(code: String): Result<Boolean> = withContext(Dispatchers.IO) {
return@withContext runCatching { 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) { when (response.status) {
HttpStatusCode.OK -> true HttpStatusCode.OK -> true
else -> error(response.bodyAsText()) HttpStatusCode.Unauthorized, HttpStatusCode.BadRequest -> false
else -> error("Request error: ${response.bodyAsText()}")
} }
} }
} }

View File

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