فهرست منبع

app: add sing in, start add reset password

Bobarik31p 6 روز پیش
والد
کامیت
ab50a422d9

+ 2 - 0
App/app/build.gradle.kts

@@ -81,6 +81,8 @@ dependencies {
     implementation (libs.androidx.fragment.ktx)
     implementation (libs.androidx.lifecycle.viewmodel.ktx)
     implementation (libs.androidx.lifecycle.viewmodel.compose)
+    implementation("com.squareup.okhttp3:okhttp:4.10.0")
+    implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
 
 
 }

+ 59 - 1
App/app/src/main/java/com/example/mystictale/MainActivity.kt

@@ -11,6 +11,13 @@ import com.example.mystictale.ViewModels.AuthViewModel
 import com.example.mystictale.navigation.Navigation
 import com.example.mystictale.resources.components.dateComponents.isDateValid
 import com.example.mystictale.ui.theme.MysticTaleTheme
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import okhttp3.OkHttpClient
+import okhttp3.Request
+import org.json.JSONArray
 
 class MainActivity : ComponentActivity() {
     @RequiresApi(Build.VERSION_CODES.O)
@@ -21,7 +28,14 @@ class MainActivity : ComponentActivity() {
         setContent {
             MysticTaleTheme {
 
-                Navigation()
+                //Navigation()
+                getUserIdByEmail("kbobarik@mail.ru") { userId ->
+                    if (userId != null) {
+                        println("UID пользователя: $userId")
+                    } else {
+                        println("Пользователь с таким email не найден.")
+                    }
+                }
 
 
             }
@@ -29,3 +43,47 @@ class MainActivity : ComponentActivity() {
     }
 }
 
+fun getUserIdByEmail(email: String, onResult: (String?) -> Unit) {
+    CoroutineScope(Dispatchers.IO).launch {
+        val client = OkHttpClient()
+        val url = "https://eykhnnlgofgxdzlixydt.supabase.co/auth/v1/admin/users?filter=email.eq.$email"
+        val request = Request.Builder()
+            .url(url)
+            .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV5a2hubmxnb2ZneGR6bGl4eWR0Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTcyOTIzNDg3OCwiZXhwIjoyMDQ0ODEwODc4fQ.Y_4IElNlvVo59iYJ4Wa9AJyP0wT7z3ysB50vxzL9bQU") // Добавьте ваш service_role ключ
+            .addHeader("apikey", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV5a2hubmxnb2ZneGR6bGl4eWR0Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTcyOTIzNDg3OCwiZXhwIjoyMDQ0ODEwODc4fQ.Y_4IElNlvVo59iYJ4Wa9AJyP0wT7z3ysB50vxzL9bQU") // То же самое
+            .build()
+
+        try {
+            val response = client.newCall(request).execute()
+            val responseBody = response.body?.string()
+
+            // Обработка кода ошибки
+            if (response.code != 200) {
+                println("Ошибка: ${response.code}, Сообщение: $responseBody")
+                withContext(Dispatchers.Main) {
+                    onResult(null)
+                }
+                return@launch
+            }
+
+            val userId = if (!responseBody.isNullOrEmpty()) {
+                val jsonArray = JSONArray(responseBody)
+                if (jsonArray.length() > 0) {
+                    val userObject = jsonArray.getJSONObject(0)
+                    userObject.getString("id")
+                } else null
+            } else null
+
+            withContext(Dispatchers.Main) {
+                onResult(userId)
+            }
+        } catch (e: Exception) {
+            e.printStackTrace()
+            withContext(Dispatchers.Main) {
+                onResult(null)
+            }
+        }
+    }
+}
+
+

+ 37 - 3
App/app/src/main/java/com/example/mystictale/Screen/passwordRecovery/NewPassword.kt

@@ -1,5 +1,6 @@
 package com.example.mystictale.Screen.passwordRecovery
 
+import android.widget.Toast
 import androidx.compose.foundation.Image
 import androidx.compose.foundation.gestures.detectTapGestures
 import androidx.compose.foundation.layout.Arrangement
@@ -17,6 +18,7 @@ import androidx.compose.material3.ButtonColors
 import androidx.compose.material3.MaterialTheme
 import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
 import androidx.compose.ui.Alignment
@@ -25,6 +27,7 @@ import androidx.compose.ui.draw.paint
 import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.input.pointer.pointerInput
 import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.platform.LocalFocusManager
 import androidx.compose.ui.platform.LocalSoftwareKeyboardController
 import androidx.compose.ui.res.painterResource
@@ -33,14 +36,19 @@ import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import androidx.navigation.NavController
 import com.example.mystictale.R
+import com.example.mystictale.ViewModels.AuthViewModel
+import com.example.mystictale.models.UserState
 import com.example.mystictale.navigation.Screens
+import com.example.mystictale.resources.components.Loading
 import com.example.mystictale.resources.components.PasswordTextField
 import com.example.mystictale.ui.theme.DarkPurple
 import com.example.mystictale.ui.theme.OpenSans
 
 @Composable
-fun NewPassword(navController: NavController) {
+fun NewPassword(navController: NavController, viewModel: AuthViewModel) {
+    val userState by viewModel.userState
     val focusManager = LocalFocusManager.current
+    val context = LocalContext.current
     val keyboardController = LocalSoftwareKeyboardController.current
     val password = remember {
         mutableStateOf("")
@@ -48,6 +56,10 @@ fun NewPassword(navController: NavController) {
     val repeatedPassword = remember {
         mutableStateOf("")
     }
+
+    val flag = remember {
+        mutableStateOf(false)
+    }
     Column(
         Modifier
             .fillMaxSize()
@@ -118,13 +130,16 @@ fun NewPassword(navController: NavController) {
                 PasswordTextField(
                     value = repeatedPassword.value,
                     onValueChange = { repeatedPassword.value = it },
-                    placeholder = "Повторный пароль",false
+                    placeholder = "Повторный пароль", false
                 )
             }
         }
         Box(Modifier.padding(bottom = 50.dp)) {
             Button(
-                onClick = { navController.navigate(Screens.RegistrationPassword.route) },
+                onClick = {
+                    viewModel.resetPassword(passwordUser = password.value)
+                    flag.value = true
+                },
                 modifier = Modifier
                     .width(310.dp)
                     .height(48.dp),
@@ -144,4 +159,23 @@ fun NewPassword(navController: NavController) {
 
 
     }
+    if (flag.value) {
+        when (userState) {
+            is UserState.Loading -> {
+                Loading()
+            }
+
+            is UserState.Success -> {
+                navController.navigate(Screens.Start.route)
+                flag.value = false
+            }
+
+            is UserState.Error -> {
+                val message = (userState as UserState.Error).message
+                Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
+                flag.value = false
+            }
+
+        }
+    }
 }

+ 45 - 10
App/app/src/main/java/com/example/mystictale/Screen/passwordRecovery/PasswordRecovery.kt

@@ -1,5 +1,6 @@
 package com.example.mystictale.Screen.passwordRecovery
 
+import android.widget.Toast
 import androidx.compose.foundation.Image
 import androidx.compose.foundation.gestures.detectTapGestures
 import androidx.compose.foundation.layout.Arrangement
@@ -18,6 +19,7 @@ import androidx.compose.material3.ButtonColors
 import androidx.compose.material3.MaterialTheme
 import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
 import androidx.compose.ui.Alignment
@@ -26,6 +28,7 @@ import androidx.compose.ui.draw.paint
 import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.input.pointer.pointerInput
 import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.platform.LocalFocusManager
 import androidx.compose.ui.platform.LocalSoftwareKeyboardController
 import androidx.compose.ui.res.painterResource
@@ -35,18 +38,26 @@ import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import androidx.navigation.NavController
 import com.example.mystictale.R
+import com.example.mystictale.ViewModels.AuthViewModel
+import com.example.mystictale.models.UserState
 import com.example.mystictale.navigation.Screens
 import com.example.mystictale.resources.components.GenericTextField
+import com.example.mystictale.resources.components.Loading
 import com.example.mystictale.ui.theme.DarkPurple
 import com.example.mystictale.ui.theme.OpenSans
 
 @Composable
-fun PasswordRecovery(navController: NavController) {
+fun PasswordRecovery(navController: NavController,viewModel: AuthViewModel) {
+    val userState by viewModel.userState
     val focusManager = LocalFocusManager.current
+    val context = LocalContext.current
     val keyboardController = LocalSoftwareKeyboardController.current
     val email = remember {
         mutableStateOf("")
     }
+    val flag = remember {
+        mutableStateOf(false)
+    }
     Column(
         Modifier
             .fillMaxSize()
@@ -59,7 +70,8 @@ fun PasswordRecovery(navController: NavController) {
                     keyboardController?.hide()
                     focusManager.clearFocus()
                 })
-            },
+            }
+            .padding(10.dp),
         verticalArrangement = Arrangement.SpaceBetween,
         horizontalAlignment = Alignment.CenterHorizontally
     ) {
@@ -91,7 +103,10 @@ fun PasswordRecovery(navController: NavController) {
                     )
                 }
             }
-            Column(modifier = Modifier.padding(10.dp)) {
+            Column(
+                modifier = Modifier.padding(20.dp)
+                    .padding(start = 15.dp, end = 5.dp)
+            ) {
                 Spacer(modifier = Modifier.height(30.dp))
                 Text(
                     text = "Сброс пароля",
@@ -100,8 +115,7 @@ fun PasswordRecovery(navController: NavController) {
                     fontSize = 24.sp,
                     color = Color.White,
                     modifier = Modifier
-                        .fillMaxWidth(0.9f)
-                        .padding(start = 30.dp),
+                        .fillMaxWidth(),
                     style = MaterialTheme.typography.headlineMedium
                 )
                 Spacer(modifier = Modifier.height(30.dp))
@@ -113,8 +127,7 @@ fun PasswordRecovery(navController: NavController) {
                     fontSize = 15.sp,
                     color = Color.White,
                     modifier = Modifier
-                        .fillMaxWidth(0.9f)
-                        .padding(start = 30.dp)
+                        .fillMaxWidth()
                 )
                 Spacer(modifier = Modifier.height(30.dp))
 
@@ -133,10 +146,13 @@ fun PasswordRecovery(navController: NavController) {
 
         Box(Modifier.padding(bottom = 50.dp)) {
             Button(
-                onClick = { navController.navigate(Screens.NewPassword.route) },
+                onClick = {
+                    viewModel.getUserIdByEmail(email.value)
+                    flag.value = true },
                 modifier = Modifier
-                    .width(310.dp)
-                    .height(48.dp),
+                    .fillMaxWidth()
+                    .height(48.dp)
+                    .padding(horizontal = 30.dp),
                 shape = RoundedCornerShape(12.dp),
                 colors = ButtonColors(
                     containerColor = DarkPurple,
@@ -153,4 +169,23 @@ fun PasswordRecovery(navController: NavController) {
 
 
     }
+    if (flag.value) {
+        when (userState) {
+            is UserState.Loading -> {
+                Loading()
+            }
+
+            is UserState.Success -> {
+                navController.navigate(Screens.NewPassword.route)
+                flag.value = false
+            }
+
+            is UserState.Error -> {
+                val message = (userState as UserState.Error).message
+                Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
+                flag.value = false
+            }
+
+        }
+    }
 }

+ 111 - 13
App/app/src/main/java/com/example/mystictale/Screen/signIn/SignIn.kt

@@ -1,5 +1,7 @@
 package com.example.mystictale.Screen.signIn
 
+import android.util.Patterns
+import android.widget.Toast
 import androidx.compose.foundation.Image
 import androidx.compose.foundation.clickable
 import androidx.compose.foundation.gestures.detectTapGestures
@@ -18,6 +20,7 @@ import androidx.compose.material3.ButtonColors
 import androidx.compose.material3.MaterialTheme
 import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
 import androidx.compose.ui.Alignment
@@ -26,6 +29,7 @@ import androidx.compose.ui.draw.paint
 import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.input.pointer.pointerInput
 import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.platform.LocalFocusManager
 import androidx.compose.ui.platform.LocalSoftwareKeyboardController
 import androidx.compose.ui.res.painterResource
@@ -35,23 +39,37 @@ import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import androidx.navigation.NavController
 import com.example.mystictale.R
+import com.example.mystictale.ViewModels.AuthViewModel
+import com.example.mystictale.models.UserState
 import com.example.mystictale.navigation.Screens
 import com.example.mystictale.resources.components.GenericTextField
+import com.example.mystictale.resources.components.Loading
 import com.example.mystictale.resources.components.PasswordTextField
 import com.example.mystictale.ui.theme.DarkPurple
 import com.example.mystictale.ui.theme.Grey
 import com.example.mystictale.ui.theme.OpenSans
 
 @Composable
-fun SignIn(navController: NavController) {
+fun SignIn(navController: NavController, viewModel: AuthViewModel) {
     val focusManager = LocalFocusManager.current
     val keyboardController = LocalSoftwareKeyboardController.current
+    val context = LocalContext.current
     val email = remember {
         mutableStateOf("")
     }
     val password = remember {
         mutableStateOf("")
     }
+    val flagEmail = remember {
+        mutableStateOf(false)
+    }
+    val flagPassword = remember {
+        mutableStateOf(false)
+    }
+    val userState by viewModel.userState
+    val flag = remember {
+        mutableStateOf(false)
+    }
     Column(
         Modifier
             .fillMaxSize()
@@ -64,8 +82,7 @@ fun SignIn(navController: NavController) {
                     keyboardController?.hide()
                     focusManager.clearFocus()
                 })
-            }
-            .padding(10.dp),
+            },
         verticalArrangement = Arrangement.SpaceBetween,
         horizontalAlignment = Alignment.CenterHorizontally
     ) {
@@ -96,7 +113,11 @@ fun SignIn(navController: NavController) {
                     )
                 }
             }
-            Column(modifier = Modifier.padding(10.dp)) {
+            Column(
+                modifier = Modifier
+                    .padding(20.dp)
+                    .padding(start = 15.dp, end = 5.dp)
+            ) {
                 Spacer(modifier = Modifier.height(30.dp))
                 Text(
                     text = "Авторизация",
@@ -105,21 +126,59 @@ fun SignIn(navController: NavController) {
                     fontSize = 24.sp,
                     color = Color.White,
                     modifier = Modifier
-                        .fillMaxWidth(0.9f)
-                        .padding(start = 35.dp),
+                        .fillMaxWidth(),
                     style = MaterialTheme.typography.headlineMedium
                 )
                 Spacer(modifier = Modifier.height(30.dp))
 
                 GenericTextField(
                     email.value,
-                    { email.value = it },
+                    {
+                        email.value = it
+                        flagEmail.value = false
+                    },
                     "E-mail",
                     KeyboardType.Email,
-                    false
+                    flagEmail.value
                 )
+                if (flagEmail.value) {
+                    if (email.value.isEmpty()) {
+                        Text(
+                            "Введите почту",
+                            fontWeight = FontWeight.Light,
+                            fontFamily = OpenSans,
+                            fontSize = 13.sp,
+                            color = Color.Red
+                        )
+                    } else if (!Patterns.EMAIL_ADDRESS.matcher(email.value).matches()) {
+                        Text(
+                            "Неверная почта",
+                            fontWeight = FontWeight.Light,
+                            fontFamily = OpenSans,
+                            fontSize = 13.sp,
+                            color = Color.Red
+                        )
+                    }
+                }
                 Spacer(modifier = Modifier.height(30.dp))
-                PasswordTextField(password.value, { password.value = it }, "Пароль",false)
+                PasswordTextField(
+                    password.value,
+                    {
+                        password.value = it
+                        flagPassword.value = false
+                    },
+                    "Пароль",
+                    flagPassword.value
+                )
+                if (flagPassword.value) {
+                    Text(
+                        "Введите пароль",
+                        fontWeight = FontWeight.Light,
+                        fontFamily = OpenSans,
+                        fontSize = 13.sp,
+                        color = Color.Red
+                    )
+                }
                 Spacer(modifier = Modifier.height(30.dp))
                 Text(
                     "Забыли пароль?",
@@ -128,7 +187,6 @@ fun SignIn(navController: NavController) {
                     fontWeight = FontWeight.Light,
                     color = Grey,
                     modifier = Modifier
-                        .padding(start = 35.dp)
                         .clickable { navController.navigate(Screens.PasswordRecovery.route) }
                 )
             }
@@ -136,10 +194,31 @@ fun SignIn(navController: NavController) {
 
         Box(Modifier.padding(bottom = 50.dp)) {
             Button(
-                onClick = { },
+                onClick = {
+                    if (!Patterns.EMAIL_ADDRESS.matcher(email.value)
+                            .matches() || email.value.isEmpty()
+                    ) {
+                        flagEmail.value = true
+                    }
+                    if (password.value.isEmpty()) {
+                        flagPassword.value = true
+                    }
+                    if (Patterns.EMAIL_ADDRESS.matcher(email.value)
+                            .matches() && email.value.isNotEmpty() && password.value.isNotEmpty()
+                    ) {
+                        viewModel.onSignInEmailPassword(
+                            emailUser = email.value,
+                            passwordUser = password.value,
+                            context = context
+                        )
+                        flag.value = true
+
+                    }
+                },
                 modifier = Modifier
-                    .width(310.dp)
-                    .height(48.dp),
+                    .fillMaxWidth()
+                    .height(48.dp)
+                    .padding(horizontal = 30.dp),
                 shape = RoundedCornerShape(12.dp),
                 colors = ButtonColors(
                     containerColor = DarkPurple,
@@ -156,4 +235,23 @@ fun SignIn(navController: NavController) {
 
 
     }
+    if (flag.value) {
+        when (userState) {
+            is UserState.Loading -> {
+                Loading()
+            }
+
+            is UserState.Success -> {
+                navController.navigate(Screens.Start.route)
+                flag.value = false
+            }
+
+            is UserState.Error -> {
+                val message = (userState as UserState.Error).message
+                Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
+                flag.value = false
+            }
+
+        }
+    }
 }

+ 92 - 3
App/app/src/main/java/com/example/mystictale/ViewModels/AuthViewModel.kt

@@ -13,11 +13,18 @@ import com.example.mystictale.models.UserState
 import com.example.mystictale.resources.SharedPreferenceHelper
 import io.github.jan.supabase.auth.auth
 import io.github.jan.supabase.auth.providers.builtin.Email
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import okhttp3.OkHttpClient
+import okhttp3.Request
+import org.json.JSONObject
 
-class AuthViewModel: ViewModel() {
+class AuthViewModel : ViewModel() {
     private val _userState = mutableStateOf<UserState>(UserState.Loading)
     val userState: State<UserState> = _userState
+    private var uid by mutableStateOf<String?>(null)
     var emailUser by mutableStateOf<String?>(null)
     var passwordUser by mutableStateOf<String?>(null)
     var dateOfBirth by mutableStateOf<String?>(null)
@@ -33,18 +40,59 @@ class AuthViewModel: ViewModel() {
                 }
                 saveToken(context)
                 _userState.value = UserState.Success("Success sing up")
-                Log.d("my_tag","Success sing up")
+                Log.d("my_tag", "Success sing up")
 
 
             } catch (e: Exception) {
                 _userState.value = UserState.Error("Error: ${e.message}")
-                Log.d("my_tag",e.message!!)
+                Log.d("my_tag", e.message!!)
 
             }
 
         }
     }
 
+    fun onSignInEmailPassword(context: Context, emailUser: String, passwordUser: String) {
+        viewModelScope.launch {
+            try {
+                _userState.value = UserState.Loading
+                SupabaseConnect.supabase.auth.signInWith(Email) {
+                    email = emailUser
+                    password = passwordUser
+                }
+
+                saveToken(context)
+                _userState.value = UserState.Success("Success sing in")
+                Log.d("my_tag", "Success sing in")
+
+            } catch (e: Exception) {
+                _userState.value = UserState.Error("Error: ${e.message}")
+                Log.d("my_tag", e.message!!)
+            }
+        }
+    }
+
+    fun resetPassword(passwordUser: String){
+        viewModelScope.launch {
+            try {
+                Log.d("my_tag", uid!!)
+                _userState.value = UserState.Loading
+                SupabaseConnect.supabase.auth.admin.updateUserById(
+                    uid!!
+                ){
+                    mapOf("password" to passwordUser)
+                }
+                _userState.value = UserState.Success("Success changed password")
+                Log.d("my_tag", "Success changed password")
+
+            } catch (e: Exception) {
+                _userState.value = UserState.Error("Error: ${e.message}")
+                Log.d("my_tag", e.message!!)
+            }
+        }
+
+    }
+
     private fun saveToken(context: Context) {
         viewModelScope.launch {
             val accessToken = SupabaseConnect.supabase.auth.currentAccessTokenOrNull()
@@ -59,7 +107,48 @@ class AuthViewModel: ViewModel() {
 
     }
 
+    fun getUserIdByEmail(email: String) {
+        CoroutineScope(Dispatchers.IO).launch {
+            val client = OkHttpClient()
+            val request = Request.Builder()
+                .url("https://eykhnnlgofgxdzlixydt.supabase.co/auth/v1/admin/users?filter=email.eq.$email")
+                .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV5a2hubmxnb2ZneGR6bGl4eWR0Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTcyOTIzNDg3OCwiZXhwIjoyMDQ0ODEwODc4fQ.Y_4IElNlvVo59iYJ4Wa9AJyP0wT7z3ysB50vxzL9bQU")
+                .addHeader("apikey", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV5a2hubmxnb2ZneGR6bGl4eWR0Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTcyOTIzNDg3OCwiZXhwIjoyMDQ0ODEwODc4fQ.Y_4IElNlvVo59iYJ4Wa9AJyP0wT7z3ysB50vxzL9bQU")
+                .build()
+
+            try {
+                _userState.value = UserState.Loading
+                val response = client.newCall(request).execute()
+                val responseBody = response.body?.string()
+
+                val userId = if (responseBody != null) {
+                    val json = JSONObject(responseBody)
+                    if (json.has("users")) {
+                        val users = json.getJSONArray("users")
+                        if (users.length() > 0) {
+                            users.getJSONObject(0).getString("id")
+                        } else null
+                    } else null
+                } else null
+                if(userId==null)
+                {
+                    _userState.value = UserState.Error("Unknow user")
+                    Log.d("my_tag", "Unknow user")
+                }
+                else{
+                    uid = userId
+                    Log.d("my_tag", uid!!)
+                    _userState.value = UserState.Success("Success reset password")
+                    Log.d("my_tag", "Success reset password")
+                }
 
+            } catch (e: Exception) {
+                e.printStackTrace()
+                _userState.value = UserState.Error("Error: ${e.message}")
+                Log.d("my_tag", e.message!!)
+            }
+        }
+    }
 
 
 }

+ 3 - 3
App/app/src/main/java/com/example/mystictale/navigation/Navigation.kt

@@ -51,15 +51,15 @@ fun Navigation() {
         }
         composable(Screens.SingIn.route)
         {
-            SignIn(navController)
+            SignIn(navController,viewModel)
         }
         composable(Screens.PasswordRecovery.route)
         {
-            PasswordRecovery(navController)
+            PasswordRecovery(navController,viewModel)
         }
         composable(Screens.NewPassword.route)
         {
-            NewPassword(navController)
+            NewPassword(navController,viewModel)
         }
 
     }