auth implemented
This commit is contained in:
parent
6b1a4e5e96
commit
9dac68d0db
@ -4,16 +4,18 @@ import RetrofitClient
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.nto_minipigs.Retrofit.Models.Auth
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class LoginViewModel :ViewModel() {
|
||||
private val serviceApi = RetrofitClient.apiService
|
||||
|
||||
fun getData(status: String, func: () -> Unit) {
|
||||
fun getData(login: String, password: String, func: () -> Unit, dataStore: UserData) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response = serviceApi.auth(status)
|
||||
Log.d("auth:", response.toString())
|
||||
val auth = Auth(login, password)
|
||||
val response = serviceApi.login(auth)
|
||||
dataStore.updateToken(response.body()?.token.toString())
|
||||
func()
|
||||
} catch (e: Exception) {
|
||||
Log.d("exception:", e.toString())
|
||||
|
@ -9,6 +9,7 @@ import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.datastore.core.DataStore
|
||||
@ -43,15 +44,16 @@ class MainActivity : ComponentActivity() {
|
||||
val navController = rememberNavController()
|
||||
val loginViewModel = ViewModelProvider(this)[LoginViewModel::class.java]
|
||||
|
||||
val token by dataStore.token.collectAsStateWithLifecycle("")
|
||||
val token by dataStore.token.collectAsState("")
|
||||
|
||||
Log.d("tokenn", token.toString())
|
||||
Log.d("token", token.toString())
|
||||
|
||||
NavHost(
|
||||
navController = navController,
|
||||
startDestination = Login
|
||||
startDestination = if(token == null) Login else Main
|
||||
// startDestination = Login
|
||||
) {
|
||||
composable<Login> { LoginScreen( onNavigateToMain = { navController.navigate(route = Main)}, viewModel = loginViewModel, dataStore = dataStore ) }
|
||||
composable<Login> { LoginScreen( onNavigateToMain = { navController.popBackStack(); navController.navigate(route = Main) }, viewModel = loginViewModel, dataStore = dataStore ) }
|
||||
composable<Main> { MainScreen() }
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
package com.example.nto_minipigs.Retrofit
|
||||
|
||||
import com.example.nto_minipigs.Retrofit.Models.Auth
|
||||
import com.example.nto_minipigs.Retrofit.Models.Data
|
||||
import okhttp3.ResponseBody
|
||||
import retrofit2.Response
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.PATCH
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.Path
|
||||
|
||||
public interface ApiService {
|
||||
@ -16,4 +19,7 @@ public interface ApiService {
|
||||
|
||||
@PATCH("/api/{LOGIN}/open")
|
||||
suspend fun open(@Body data: Data, @Path("LOGIN") login: String): ResponseBody
|
||||
|
||||
@POST("/api/login")
|
||||
suspend fun login(@Body data: Auth): Response<Data>
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.example.nto_minipigs.Retrofit.Models
|
||||
|
||||
data class Auth(
|
||||
val login: String,
|
||||
val password: String
|
||||
)
|
@ -1,5 +1,5 @@
|
||||
package com.example.nto_minipigs.Retrofit.Models
|
||||
|
||||
data class Data(
|
||||
val value: String
|
||||
val token: String
|
||||
)
|
@ -4,6 +4,7 @@ data class User(
|
||||
val id: Int,
|
||||
val lastVisit: String,
|
||||
val login: String,
|
||||
val password: String,
|
||||
val name: String,
|
||||
val photo: String,
|
||||
val position: String
|
||||
|
@ -63,10 +63,7 @@ fun LoginScreen( onNavigateToMain: () -> Unit, viewModel: LoginViewModel, dataSt
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
viewModel.getData(login, onNavigateToMain)
|
||||
cor.launch {
|
||||
dataStore.updateToken("yenis")
|
||||
}
|
||||
viewModel.getData(login, password, onNavigateToMain, dataStore)
|
||||
},
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user