day2_commit_2_UI_added_password_cheak_and_basic_auth_(test)
This commit is contained in:
parent
c68417891c
commit
06b7ab6608
@ -3,12 +3,16 @@ package ru.myitschool.work.api
|
||||
import retrofit2.Response
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.PATCH
|
||||
import retrofit2.http.Path
|
||||
|
||||
interface ApiService {
|
||||
@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")
|
||||
suspend fun getUserInfo(@Path("login") login: String): Response<Map<String, Any>> // Возвращаем Map вместо UserInfo
|
||||
|
@ -52,7 +52,8 @@ class LoginFragment : Fragment(R.layout.fragment_login) {
|
||||
|
||||
binding.login.setOnClickListener {
|
||||
val username = binding.username.text.toString()
|
||||
performLogin(username) // Вызываем метод performLogin
|
||||
val password = binding.password.text.toString() // Получаем пароль
|
||||
performLogin(username, password) // Передаем пароль в метод performLogin
|
||||
}
|
||||
|
||||
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 {
|
||||
viewModel.authenticate(username) // Вызываем метод authenticate из ViewModel
|
||||
viewModel.authenticate(username, password) // Передаем пароль в метод authenticate
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import okhttp3.Credentials
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import ru.myitschool.work.SessionManager
|
||||
@ -30,10 +31,11 @@ class LoginViewModel @Inject constructor(
|
||||
.create(ApiService::class.java)
|
||||
}
|
||||
|
||||
fun authenticate(username: String) {
|
||||
fun authenticate(username: String, password: String) {
|
||||
if (isValidUsername(username)) {
|
||||
viewModelScope.launch {
|
||||
val response = apiService.authenticate(username)
|
||||
val credentials = Credentials.basic(username, password) // Создаем Basic Auth заголовок
|
||||
val response = apiService.authenticate(username, credentials) // Передаем заголовок в запрос
|
||||
if (response.isSuccessful) {
|
||||
SessionManager.userLogin = username
|
||||
_state.value = LoginState(success = true)
|
||||
|
Loading…
x
Reference in New Issue
Block a user