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