basic auth - mne ploho
This commit is contained in:
parent
40825f2416
commit
eae3035683
@ -4,16 +4,24 @@ import okhttp3.ResponseBody
|
||||
import retrofit2.Response
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.PATCH
|
||||
import retrofit2.http.Path
|
||||
import retrofit2.http.Query
|
||||
|
||||
interface LoginApi {
|
||||
@GET("{login}/auth")
|
||||
suspend fun auth(@Path("login") login: String): Response<ResponseBody>
|
||||
@GET("auth")
|
||||
suspend fun auth(
|
||||
@Header("Authorization") authHeader: String,
|
||||
@Query("login") login: String
|
||||
): Response<ResponseBody>
|
||||
|
||||
@GET("{login}/info")
|
||||
suspend fun info(@Path("login") login: String): PersonInfoDto
|
||||
@GET("info")
|
||||
suspend fun info(
|
||||
@Header("Authorization") authHeader: String,
|
||||
@Query("login") login: String
|
||||
): Response<ResponseBody>
|
||||
|
||||
@PATCH("{login}/open")
|
||||
suspend fun open(@Path("login") login: String, @Body request: OpenWithCodeRequest): Response<ResponseBody>
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.gson.GsonBuilder
|
||||
import createAuthHeader
|
||||
import createRetrofit
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@ -30,6 +32,9 @@ class LoginViewModel @Inject constructor(
|
||||
private val _savedUsername = MutableStateFlow<String?>(null)
|
||||
val savedUsername = _savedUsername.asStateFlow()
|
||||
|
||||
private val retrofit = createRetrofit("your_username", "your_password")
|
||||
val loginApi = retrofit.create(LoginApi::class.java)
|
||||
|
||||
private val _stateP = MutableStateFlow(PasswordState())
|
||||
val stateP = _stateP.asStateFlow()
|
||||
|
||||
@ -57,7 +62,8 @@ class LoginViewModel @Inject constructor(
|
||||
fun tryLogin(username: String, password : String, onSuccess: () -> Unit) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val resp = api.auth(username)
|
||||
val authHeader = createAuthHeader(username = username, password = password)
|
||||
val resp = loginApi.auth(authHeader, username)
|
||||
if (resp.code() != 200) {
|
||||
throw HttpException(resp)
|
||||
}
|
||||
|
@ -3,4 +3,4 @@ package ru.myitschool.work.ui.main
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class MainDestination(val username: String)
|
||||
data class MainDestination(val username: String, val password: String)
|
@ -29,22 +29,25 @@ class MainFragment: Fragment(R.layout.fragment_main) {
|
||||
|
||||
val username = findNavController().currentBackStackEntry?.toRoute<MainDestination>()?.username
|
||||
username?.let { user ->
|
||||
viewModel.loadPersonInfo(user)
|
||||
binding.refresh.setOnClickListener { viewModel.loadPersonInfo(user) }
|
||||
binding.logout.setOnClickListener {
|
||||
viewModel.logout {
|
||||
findNavController().apply {
|
||||
popBackStack<MainDestination>(true)
|
||||
navigate(LoginDestination)
|
||||
val password = findNavController().currentBackStackEntry?.toRoute<MainDestination>()?.password
|
||||
password?.let { password ->
|
||||
viewModel.loadPersonInfo(user, password)
|
||||
binding.refresh.setOnClickListener { viewModel.loadPersonInfo(user) }
|
||||
binding.logout.setOnClickListener {
|
||||
viewModel.logout {
|
||||
findNavController().apply {
|
||||
popBackStack<MainDestination>(true)
|
||||
navigate(LoginDestination)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
binding.scan.setOnClickListener {
|
||||
findNavController().navigate(QrResultDestination(user))
|
||||
findNavController().navigate(QrScanDestination)
|
||||
}
|
||||
binding.admin.setOnClickListener{
|
||||
findNavController().navigate(AdminDestination(user))
|
||||
binding.scan.setOnClickListener {
|
||||
findNavController().navigate(QrResultDestination(user))
|
||||
findNavController().navigate(QrScanDestination)
|
||||
}
|
||||
binding.admin.setOnClickListener {
|
||||
findNavController().navigate(AdminDestination(user))
|
||||
}
|
||||
}
|
||||
}
|
||||
subscribe()
|
||||
|
@ -5,6 +5,8 @@ import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.gson.GsonBuilder
|
||||
import createAuthHeader
|
||||
import createRetrofit
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@ -31,10 +33,14 @@ class MainViewModel @Inject constructor(
|
||||
private val dfo = SimpleDateFormat("yyyy-MM-dd HH:mm")
|
||||
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 {
|
||||
try {
|
||||
val info = api.info(username)
|
||||
val authHeader = createAuthHeader(username = username, password)
|
||||
val info = loginApi.auth(authHeader, username)
|
||||
_state.update {
|
||||
MainState(
|
||||
fullName = info.name,
|
||||
|
Loading…
x
Reference in New Issue
Block a user