Авторизация и регистрация через API готовы (вроде как)
This commit is contained in:
parent
ad99fc9748
commit
423a7c4c7e
@ -22,6 +22,11 @@ interface MyServerService {
|
||||
@Body user: UserModelRemote
|
||||
) : TokenModelRemote
|
||||
|
||||
@POST("check/login")
|
||||
suspend fun checkLogin(
|
||||
@Body user: UserModelRemote
|
||||
) :UserModelRemote?
|
||||
|
||||
|
||||
companion object {
|
||||
private const val BASE_URL = "https://10.0.2.2:80/api/"
|
||||
|
@ -5,16 +5,17 @@ import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class TokenModelRemote(
|
||||
val access_token: String = ""
|
||||
val access_token: String = "",
|
||||
val message: String = ""
|
||||
)
|
||||
|
||||
|
||||
@Serializable
|
||||
data class UserModelRemote(
|
||||
val id: Int = 0,
|
||||
val login: String = "",
|
||||
val password:String = "",
|
||||
val role:String = ""
|
||||
val role:String = "",
|
||||
val message: String = ""
|
||||
)
|
||||
|
||||
fun UserModelRemote.toUserModel(): UserModel = UserModel(
|
||||
|
@ -2,6 +2,7 @@ package com.example.shawarma.data.api.repos
|
||||
|
||||
import com.example.shawarma.data.api.MyServerService
|
||||
import com.example.shawarma.data.api.models.TokenModelRemote
|
||||
import com.example.shawarma.data.api.models.UserModelRemote
|
||||
import com.example.shawarma.data.api.models.toUserModelRemote
|
||||
import com.example.shawarma.data.models.UserModel
|
||||
import javax.inject.Inject
|
||||
@ -16,4 +17,8 @@ class RestUserRepository @Inject constructor(
|
||||
suspend fun getToken(user: UserModel): TokenModelRemote {
|
||||
return service.getToken(user.toUserModelRemote())
|
||||
}
|
||||
|
||||
suspend fun checkLogin(user: UserModel): UserModelRemote? {
|
||||
return service.checkLogin(user.toUserModelRemote())
|
||||
}
|
||||
}
|
@ -6,7 +6,6 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.shawarma.data.api.repos.RestUserRepository
|
||||
import com.example.shawarma.data.models.UserModel
|
||||
import com.example.shawarma.data.repos.UserRepository
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
@ -15,7 +14,6 @@ import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class UserViewModel @Inject constructor(
|
||||
private val userRepository: UserRepository,
|
||||
private val restUserRepository: RestUserRepository
|
||||
) : ViewModel() {
|
||||
private val _userModel = MutableLiveData<UserModel?>()
|
||||
@ -72,18 +70,17 @@ class UserViewModel @Inject constructor(
|
||||
}
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.Main) {
|
||||
userRepository.login(login, password).collect() { user ->
|
||||
if (user == null) {
|
||||
val user_response = restUserRepository.checkLogin(UserModel(null, login, password, ""))
|
||||
if (user_response != null) {
|
||||
if (user_response.login.isNullOrEmpty()) {
|
||||
if (login == "admin" && password == "admin") {
|
||||
restUserRepository.insert(UserModel(null, login, password, "ADMIN"))
|
||||
_registrationState.postValue(true)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
restUserRepository.insert(UserModel(null, login, password, "USER"))
|
||||
_registrationState.postValue(true)
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (registrationState.value != true) {
|
||||
_registrationState.postValue(false)
|
||||
}
|
||||
@ -93,6 +90,7 @@ class UserViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun calmRegistrationState() {
|
||||
_registrationState.postValue(null)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user