Bladeren bron

Аунтефикация и регистрация

Blueris 1 maand geleden
bovenliggende
commit
32f8c54b7a

+ 2 - 0
app/src/main/java/com/example/test/view/ui/view/MainActivity.kt

@@ -16,6 +16,7 @@ import androidx.compose.ui.Modifier
 import androidx.compose.ui.tooling.preview.Preview
 import com.example.test.view.ui.view.theme.TestTheme
 import com.example.test.view.ui.view.components.FilmsList
+import com.example.test.view.ui.view.components.auth
 import com.example.test.view.ui.view.theme.TestTheme
 
 class MainActivity : ComponentActivity() {
@@ -28,6 +29,7 @@ class MainActivity : ComponentActivity() {
                     verticalArrangement = Arrangement.Center,
                     horizontalAlignment = Alignment.CenterHorizontally
                 ) {
+                   auth()
                    FilmsList()
                 }
                 }

+ 64 - 0
app/src/main/java/com/example/test/view/ui/view/MainViewModel.kt

@@ -0,0 +1,64 @@
+package com.example.test.view.ui.view
+
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.viewModelScope
+import com.example.test.domain.utlls.Constant
+import io.github.jan.supabase.gotrue.auth
+import io.github.jan.supabase.gotrue.providers.builtin.Email
+import io.github.jan.supabase.gotrue.providers.builtin.OTP
+import io.github.jan.supabase.postgrest.from
+import kotlinx.coroutines.launch
+
+class MainViewModel():ViewModel() {
+
+    fun onSignInEmailCode(emailUser: String) {
+        viewModelScope.launch {
+            try {
+                Constant.supabase.auth.signInWith(OTP) {
+                    email = emailUser
+                    createUser = false
+                }
+
+            } catch (e: Exception) {
+                println(e.message.toString())
+
+            }
+
+        }
+    }
+
+    fun onSignInEmailPassword(emailUser: String, passwordUser: String) {
+        viewModelScope.launch {
+            try {
+                val user = Constant.supabase.auth.signInWith(Email) {
+                    email = emailUser
+                    password = passwordUser
+                }
+                println(user.toString())
+                println(Constant.supabase.auth.currentUserOrNull()!!.id)
+                println("Success")
+            } catch (e: Exception) {
+                println("Error")
+                println(e.message.toString())
+            }
+        }
+    }
+    fun onSignUpEmail(emailUser: String, passwordUser: String) {
+        viewModelScope.launch {
+            try{
+                var  user =  Constant.supabase.auth.signUpWith(Email) {
+                    email = emailUser
+                    password = passwordUser
+                }
+                println(user.toString())
+                println(Constant.supabase.auth.currentUserOrNull()!!.id)
+                println("Success")
+            }
+            catch (e: Exception) {
+                println("Error")
+                println(e.message.toString())
+            }
+
+        }
+    }
+}

+ 79 - 0
app/src/main/java/com/example/test/view/ui/view/components/auth.kt

@@ -0,0 +1,79 @@
+package com.example.test.view.ui.view.components
+
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.padding
+import androidx.compose.material3.Button
+import androidx.compose.material3.Icon
+import androidx.compose.material3.IconButton
+import androidx.compose.material3.Text
+import androidx.compose.material3.TextField
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.res.painterResource
+import androidx.compose.ui.text.TextStyle
+import androidx.compose.ui.text.input.PasswordVisualTransformation
+import androidx.compose.ui.text.input.VisualTransformation
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import com.example.test.view.ui.view.MainViewModel
+import com.example.test.R
+
+
+@Preview
+@Composable
+fun auth() {
+    val viewModel = MainViewModel()
+    val email = remember { mutableStateOf("") }
+    val password = remember { mutableStateOf("") }
+    Column(
+        Modifier.padding(top = 30.dp, start = 15.dp, end = 15.dp),
+        verticalArrangement = Arrangement.Center,
+        horizontalAlignment = Alignment.CenterHorizontally
+
+    ) {
+        TextField(
+            value = email.value,
+            textStyle = TextStyle(fontSize = 25.sp),
+            onValueChange = { newText -> email.value = newText }
+        )
+        var passwordVisibility: Boolean by remember { mutableStateOf(false) }
+        TextField(value = password.value,
+            visualTransformation = if (passwordVisibility) VisualTransformation.None else PasswordVisualTransformation(),
+            leadingIcon = {
+                IconButton(onClick = {
+                    passwordVisibility = !passwordVisibility
+                }) {
+                    if(passwordVisibility) {
+                        Icon(
+                            painter = painterResource(id = R.drawable.baseline_person_24),
+                            contentDescription = ""
+                        )
+                    }
+                    else {
+                        Icon(
+                            painter = painterResource(id = R.drawable.baseline_person_off_24),
+                            contentDescription = ""
+                        )
+                    }
+                }
+            },
+            onValueChange = { newText -> password.value = newText })
+        Button(onClick = {
+            viewModel.onSignInEmailPassword(email.value,password.value)
+        }){
+            Text("SigIn", fontSize = 25.sp)
+        }
+        Button(onClick = {
+            viewModel.onSignUpEmail(email.value,password.value)
+        }){
+            Text("SigUp", fontSize = 25.sp)
+        }
+    }
+}

+ 5 - 0
app/src/main/res/drawable/baseline_person_24.xml

@@ -0,0 +1,5 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
+      
+    <path android:fillColor="@android:color/white" android:pathData="M12,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4 -4,1.79 -4,4 1.79,4 4,4zM12,14c-2.67,0 -8,1.34 -8,4v2h16v-2c0,-2.66 -5.33,-4 -8,-4z"/>
+    
+</vector>

+ 5 - 0
app/src/main/res/drawable/baseline_person_off_24.xml

@@ -0,0 +1,5 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
+      
+    <path android:fillColor="@android:color/white" android:pathData="M8.65,5.82C9.36,4.72 10.6,4 12,4c2.21,0 4,1.79 4,4c0,1.4 -0.72,2.64 -1.82,3.35L8.65,5.82zM20,17.17c-0.02,-1.1 -0.63,-2.11 -1.61,-2.62c-0.54,-0.28 -1.13,-0.54 -1.77,-0.76L20,17.17zM21.19,21.19L2.81,2.81L1.39,4.22l8.89,8.89c-1.81,0.23 -3.39,0.79 -4.67,1.45C4.61,15.07 4,16.1 4,17.22V20h13.17l2.61,2.61L21.19,21.19z"/>
+    
+</vector>