day2_commit_2_UI_added_password_cheak_and_basic_auth_(test)

This commit is contained in:
Terebov_Maksim 2025-02-19 10:39:48 +03:00
parent c68417891c
commit 06b7ab6608
3 changed files with 13 additions and 6 deletions

View File

@ -3,12 +3,16 @@ package ru.myitschool.work.api
import retrofit2.Response import retrofit2.Response
import retrofit2.http.Body import retrofit2.http.Body
import retrofit2.http.GET import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.PATCH import retrofit2.http.PATCH
import retrofit2.http.Path import retrofit2.http.Path
interface ApiService { interface ApiService {
@GET("api/{login}/auth") @GET("api/{login}/auth")
suspend fun authenticate(@Path("login") login: String): Response<Unit> suspend fun authenticate(
@Path("login") login: String,
@Header("Authorization") authorization: String // Добавляем заголовок Authorization
): Response<Unit>
@GET("api/{login}/info") @GET("api/{login}/info")
suspend fun getUserInfo(@Path("login") login: String): Response<Map<String, Any>> // Возвращаем Map вместо UserInfo suspend fun getUserInfo(@Path("login") login: String): Response<Map<String, Any>> // Возвращаем Map вместо UserInfo

View File

@ -52,7 +52,8 @@ class LoginFragment : Fragment(R.layout.fragment_login) {
binding.login.setOnClickListener { binding.login.setOnClickListener {
val username = binding.username.text.toString() val username = binding.username.text.toString()
performLogin(username) // Вызываем метод performLogin val password = binding.password.text.toString() // Получаем пароль
performLogin(username, password) // Передаем пароль в метод performLogin
} }
binding.loading.visibleOrGone(false) binding.loading.visibleOrGone(false)
@ -72,9 +73,9 @@ class LoginFragment : Fragment(R.layout.fragment_login) {
}) })
} }
private fun performLogin(username: String) { private fun performLogin(username: String, password: String) {
lifecycleScope.launch { lifecycleScope.launch {
viewModel.authenticate(username) // Вызываем метод authenticate из ViewModel viewModel.authenticate(username, password) // Передаем пароль в метод authenticate
} }
} }

View File

@ -8,6 +8,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import okhttp3.Credentials
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory import retrofit2.converter.gson.GsonConverterFactory
import ru.myitschool.work.SessionManager import ru.myitschool.work.SessionManager
@ -30,10 +31,11 @@ class LoginViewModel @Inject constructor(
.create(ApiService::class.java) .create(ApiService::class.java)
} }
fun authenticate(username: String) { fun authenticate(username: String, password: String) {
if (isValidUsername(username)) { if (isValidUsername(username)) {
viewModelScope.launch { viewModelScope.launch {
val response = apiService.authenticate(username) val credentials = Credentials.basic(username, password) // Создаем Basic Auth заголовок
val response = apiService.authenticate(username, credentials) // Передаем заголовок в запрос
if (response.isSuccessful) { if (response.isSuccessful) {
SessionManager.userLogin = username SessionManager.userLogin = username
_state.value = LoginState(success = true) _state.value = LoginState(success = true)