auth broken
This commit is contained in:
@@ -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"
|
||||||
|
|||||||
@@ -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)
|
||||||
codeCache = text
|
|
||||||
createAuthCode(code = 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: ВЕРНУТЬ СЕТЕВОЙ ЗАПРОС
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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()}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ package ru.myitschool.work.domain.auth
|
|||||||
|
|
||||||
import ru.myitschool.work.data.repo.AuthRepository
|
import ru.myitschool.work.data.repo.AuthRepository
|
||||||
|
|
||||||
class CheckAndSaveAuthCodeUseCase(
|
class CheckAndSaveAuthCodeUseCase(
|
||||||
private val repository: AuthRepository
|
private val repository: AuthRepository
|
||||||
) {
|
) {
|
||||||
suspend operator fun invoke(
|
suspend operator fun invoke(
|
||||||
text: String
|
text: String
|
||||||
): Result<Unit> {
|
): Result<Unit> {
|
||||||
return repository.checkAndSave(text).mapCatching { success ->
|
return repository.checkAndSave(text).mapCatching { success ->
|
||||||
if (!success) error("Code is incorrect")
|
if (!success) error("Code is incorrect")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -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()))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user