basic auth - mne ploho

This commit is contained in:
shipovnikaaa 2025-02-19 18:59:56 +03:00
parent 40825f2416
commit eae3035683
5 changed files with 46 additions and 23 deletions

View File

@ -4,15 +4,23 @@ import okhttp3.ResponseBody
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
import retrofit2.http.Query
interface LoginApi { interface LoginApi {
@GET("{login}/auth") @GET("auth")
suspend fun auth(@Path("login") login: String): Response<ResponseBody> suspend fun auth(
@Header("Authorization") authHeader: String,
@Query("login") login: String
): Response<ResponseBody>
@GET("{login}/info") @GET("info")
suspend fun info(@Path("login") login: String): PersonInfoDto suspend fun info(
@Header("Authorization") authHeader: String,
@Query("login") login: String
): Response<ResponseBody>
@PATCH("{login}/open") @PATCH("{login}/open")
suspend fun open(@Path("login") login: String, @Body request: OpenWithCodeRequest): Response<ResponseBody> suspend fun open(@Path("login") login: String, @Body request: OpenWithCodeRequest): Response<ResponseBody>

View File

@ -5,6 +5,8 @@ import android.util.Log
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import createAuthHeader
import createRetrofit
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@ -30,6 +32,9 @@ class LoginViewModel @Inject constructor(
private val _savedUsername = MutableStateFlow<String?>(null) private val _savedUsername = MutableStateFlow<String?>(null)
val savedUsername = _savedUsername.asStateFlow() val savedUsername = _savedUsername.asStateFlow()
private val retrofit = createRetrofit("your_username", "your_password")
val loginApi = retrofit.create(LoginApi::class.java)
private val _stateP = MutableStateFlow(PasswordState()) private val _stateP = MutableStateFlow(PasswordState())
val stateP = _stateP.asStateFlow() val stateP = _stateP.asStateFlow()
@ -57,7 +62,8 @@ class LoginViewModel @Inject constructor(
fun tryLogin(username: String, password : String, onSuccess: () -> Unit) { fun tryLogin(username: String, password : String, onSuccess: () -> Unit) {
viewModelScope.launch { viewModelScope.launch {
try { try {
val resp = api.auth(username) val authHeader = createAuthHeader(username = username, password = password)
val resp = loginApi.auth(authHeader, username)
if (resp.code() != 200) { if (resp.code() != 200) {
throw HttpException(resp) throw HttpException(resp)
} }

View File

@ -3,4 +3,4 @@ package ru.myitschool.work.ui.main
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
data class MainDestination(val username: String) data class MainDestination(val username: String, val password: String)

View File

@ -29,7 +29,9 @@ class MainFragment: Fragment(R.layout.fragment_main) {
val username = findNavController().currentBackStackEntry?.toRoute<MainDestination>()?.username val username = findNavController().currentBackStackEntry?.toRoute<MainDestination>()?.username
username?.let { user -> username?.let { user ->
viewModel.loadPersonInfo(user) val password = findNavController().currentBackStackEntry?.toRoute<MainDestination>()?.password
password?.let { password ->
viewModel.loadPersonInfo(user, password)
binding.refresh.setOnClickListener { viewModel.loadPersonInfo(user) } binding.refresh.setOnClickListener { viewModel.loadPersonInfo(user) }
binding.logout.setOnClickListener { binding.logout.setOnClickListener {
viewModel.logout { viewModel.logout {
@ -47,6 +49,7 @@ class MainFragment: Fragment(R.layout.fragment_main) {
findNavController().navigate(AdminDestination(user)) findNavController().navigate(AdminDestination(user))
} }
} }
}
subscribe() subscribe()
} }

View File

@ -5,6 +5,8 @@ import android.util.Log
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import createAuthHeader
import createRetrofit
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@ -31,10 +33,14 @@ class MainViewModel @Inject constructor(
private val dfo = SimpleDateFormat("yyyy-MM-dd HH:mm") private val dfo = SimpleDateFormat("yyyy-MM-dd HH:mm")
private val dfi= SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss") private val dfi= SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
fun loadPersonInfo(username: String) { private val retrofit = createRetrofit("your_username", "your_password")
private val loginApi = retrofit.create(LoginApi::class.java)
fun loadPersonInfo(username: String, password : String) {
viewModelScope.launch { viewModelScope.launch {
try { try {
val info = api.info(username) val authHeader = createAuthHeader(username = username, password)
val info = loginApi.auth(authHeader, username)
_state.update { _state.update {
MainState( MainState(
fullName = info.name, fullName = info.name,