Añadir crear usuario a mariadb
This commit is contained in:
@@ -31,6 +31,11 @@ android {
|
||||
buildFeatures {
|
||||
compose = true
|
||||
}
|
||||
packaging {
|
||||
resources {
|
||||
excludes += "/META-INF/{AL2.0,LGPL2.1}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -14,6 +14,7 @@ import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import com.example.saludapp.ui.LoginScreen
|
||||
import com.example.saludapp.ui.RegisterScreen
|
||||
import com.example.saludapp.ui.StartScreen
|
||||
import com.example.saludapp.ui.theme.SaludappTheme
|
||||
|
||||
@@ -29,10 +30,14 @@ class MainActivity : ComponentActivity() {
|
||||
"start" -> StartScreen {
|
||||
currentScreen = "login"
|
||||
}
|
||||
"login" -> LoginScreen {
|
||||
// Aquí iría la lógica tras login exitoso
|
||||
currentScreen = "home"
|
||||
}
|
||||
"login" -> LoginScreen(
|
||||
onLoginSuccess = { currentScreen = "home" },
|
||||
onNavigateToRegister = { currentScreen = "register" }
|
||||
)
|
||||
"register" -> RegisterScreen(
|
||||
onRegisterSuccess = { currentScreen = "login" },
|
||||
onBackToLogin = { currentScreen = "login" }
|
||||
)
|
||||
"home" -> {
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.example.saludapp.data
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.security.MessageDigest
|
||||
import java.sql.Connection
|
||||
import java.sql.DriverManager
|
||||
import java.sql.SQLException
|
||||
@@ -9,20 +10,27 @@ import java.sql.SQLException
|
||||
object DatabaseHelper {
|
||||
private const val URL = "jdbc:mariadb://mariadb.peta9.com:3306/saludapp_db"
|
||||
private const val USER = "saludapp_dba"
|
||||
private const val PASSWORD = "TU_CONTRASEÑA_AQUÍ" // TODO: Reemplazar con la contraseña real
|
||||
private const val PASSWORD = "Pedro@110387"
|
||||
|
||||
private fun hashPassword(password: String): String {
|
||||
val bytes = password.toByteArray()
|
||||
val md = MessageDigest.getInstance("SHA-256")
|
||||
val digest = md.digest(bytes)
|
||||
return digest.fold("") { str, it -> str + "%02x".format(it) }
|
||||
}
|
||||
|
||||
suspend fun validateLogin(user: String, pass: String): Boolean = withContext(Dispatchers.IO) {
|
||||
var connection: Connection? = null
|
||||
try {
|
||||
// Cargar el driver explícitamente si es necesario
|
||||
Class.forName("org.mariadb.jdbc.Driver")
|
||||
connection = DriverManager.getConnection(URL, USER, PASSWORD)
|
||||
|
||||
val hashedPass = hashPassword(pass)
|
||||
val query = "SELECT COUNT(*) FROM usuarios WHERE (username = ? OR email = ?) AND password = ?"
|
||||
connection.prepareStatement(query).use { statement ->
|
||||
statement.setString(1, user)
|
||||
statement.setString(2, user)
|
||||
statement.setString(3, pass)
|
||||
statement.setString(3, hashedPass)
|
||||
|
||||
val resultSet = statement.executeQuery()
|
||||
if (resultSet.next()) {
|
||||
@@ -40,4 +48,33 @@ object DatabaseHelper {
|
||||
}
|
||||
return@withContext false
|
||||
}
|
||||
|
||||
suspend fun registerUser(user: String, email: String, pass: String, fullName: String): Boolean = withContext(Dispatchers.IO) {
|
||||
var connection: Connection? = null
|
||||
try {
|
||||
Class.forName("org.mariadb.jdbc.Driver")
|
||||
connection = DriverManager.getConnection(URL, USER, PASSWORD)
|
||||
|
||||
val hashedPass = hashPassword(pass)
|
||||
val query = "INSERT INTO usuarios (username, email, password, nombre_completo) VALUES (?, ?, ?, ?)"
|
||||
connection.prepareStatement(query).use { statement ->
|
||||
statement.setString(1, user)
|
||||
statement.setString(2, email)
|
||||
statement.setString(3, hashedPass)
|
||||
statement.setString(4, fullName)
|
||||
|
||||
val result = statement.executeUpdate()
|
||||
return@withContext result > 0
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
try {
|
||||
connection?.close()
|
||||
} catch (e: SQLException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
return@withContext false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import com.example.saludapp.data.DatabaseHelper
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun LoginScreen(onLoginSuccess: () -> Unit) {
|
||||
fun LoginScreen(onLoginSuccess: () -> Unit, onNavigateToRegister: () -> Unit) {
|
||||
var email by remember { mutableStateOf("") }
|
||||
var password by remember { mutableStateOf("") }
|
||||
var isLoading by remember { mutableStateOf(false) }
|
||||
@@ -97,6 +97,10 @@ fun LoginScreen(onLoginSuccess: () -> Unit) {
|
||||
Text("Entrar")
|
||||
}
|
||||
}
|
||||
|
||||
TextButton(onClick = onNavigateToRegister) {
|
||||
Text("¿No tienes cuenta? Regístrate aquí")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.example.saludapp.ui
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.example.saludapp.data.DatabaseHelper
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun RegisterScreen(onRegisterSuccess: () -> Unit, onBackToLogin: () -> Unit) {
|
||||
var fullName by remember { mutableStateOf("") }
|
||||
var username by remember { mutableStateOf("") }
|
||||
var email by remember { mutableStateOf("") }
|
||||
var password by remember { mutableStateOf("") }
|
||||
var isLoading by remember { mutableStateOf(false) }
|
||||
var errorMessage by remember { mutableStateOf<String?>(null) }
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
color = MaterialTheme.colorScheme.background
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(24.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Text(
|
||||
text = "Crear Cuenta",
|
||||
style = MaterialTheme.typography.headlineMedium,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = fullName,
|
||||
onValueChange = { fullName = it },
|
||||
label = { Text("Nombre Completo") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
enabled = !isLoading
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = username,
|
||||
onValueChange = { username = it },
|
||||
label = { Text("Nombre de usuario") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
enabled = !isLoading
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = email,
|
||||
onValueChange = { email = it },
|
||||
label = { Text("Email") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
enabled = !isLoading
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = password,
|
||||
onValueChange = { password = it },
|
||||
label = { Text("Contraseña") },
|
||||
visualTransformation = PasswordVisualTransformation(),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
enabled = !isLoading
|
||||
)
|
||||
|
||||
if (errorMessage != null) {
|
||||
Text(
|
||||
text = errorMessage!!,
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier = Modifier.padding(top = 16.dp)
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(32.dp))
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
scope.launch {
|
||||
isLoading = true
|
||||
errorMessage = null
|
||||
val success = DatabaseHelper.registerUser(username, email, password, fullName)
|
||||
isLoading = false
|
||||
if (success) {
|
||||
onRegisterSuccess()
|
||||
} else {
|
||||
errorMessage = "Error al registrar. El usuario puede que ya exista."
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(56.dp),
|
||||
enabled = !isLoading && fullName.isNotEmpty() && username.isNotEmpty() && email.isNotEmpty() && password.isNotEmpty()
|
||||
) {
|
||||
if (isLoading) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(24.dp),
|
||||
color = MaterialTheme.colorScheme.onPrimary,
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
} else {
|
||||
Text("Registrarse")
|
||||
}
|
||||
}
|
||||
|
||||
TextButton(onClick = onBackToLogin) {
|
||||
Text("¿Ya tienes cuenta? Inicia sesión")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user