Scan to open on your phone
Point a camera at the QR code - it opens this exact paste, no app needed.
https://codepastes.com/zzgs6ht35x91e90wuntitledPlain Text
package com.negocio.desinfectantes.ui.auth
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.negocio.desinfectantes.R
private val LoginNavy = Color(0xFF101D35)
private val LoginCardBlue = Color(0xFF1B2D4A)
private val LoginAccentBlue = Color(0xFF2585F5)
private val LoginSoftBlue = Color(0xFFA9BEDB)
private val LoginWhite = Color(0xFFFFFFFF)
@Composable
fun LoginScreen(
onLogin: (String, String) -> Unit,
onBiometricLogin: () -> Unit,
biometricEnabled: Boolean = false,
isLoading: Boolean = false,
errorMessage: String? = null
) {
var usuario by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var mostrarPassword by remember { mutableStateOf(false) }
val puedeIngresar = usuario.isNotBlank() &&
password.isNotBlank() &&
!isLoading
Box(
modifier = Modifier
.fillMaxSize()
.background(LoginNavy)
.imePadding()
) {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(horizontal = 26.dp, vertical = 30.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Spacer(modifier = Modifier.height(20.dp))
Surface(
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(24.dp),
color = LoginWhite
) {
ImageLogo()
}
Spacer(modifier = Modifier.height(28.dp))
Text(
text = "Bienvenido",
color = LoginWhite,
fontSize = 28.sp,
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.height(6.dp))
Text(
text = "Ingresa a tu cuenta Prestige Clean",
color = LoginSoftBlue,
fontSize = 14.sp
)
Spacer(modifier = Modifier.height(30.dp))
Surface(
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(22.dp),
color = LoginCardBlue
) {
Column(
modifier = Modifier.padding(22.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
OutlinedTextField(
value = usuario,
onValueChange = { usuario = it },
modifier = Modifier.fillMaxWidth(),
label = { Text("Nombre de usuario") },
singleLine = true,
enabled = !isLoading,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Next
),
shape = RoundedCornerShape(12.dp),
colors = loginTextFieldColors()
)
OutlinedTextField(
value = password,
onValueChange = { password = it },
modifier = Modifier.fillMaxWidth(),
label = { Text("Contraseña") },
singleLine = true,
enabled = !isLoading,
visualTransformation = if (mostrarPassword) {
VisualTransformation.None
} else {
PasswordVisualTransformation()
},
trailingIcon = {
TextButton(
onClick = {
mostrarPassword = !mostrarPassword
}
) {
Text(
if (mostrarPassword) "Ocultar" else "Ver",
color = LoginSoftBlue
)
}
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Password,
imeAction = ImeAction.Done
),
shape = RoundedCornerShape(12.dp),
colors = loginTextFieldColors()
)
if (errorMessage != null) {
Text(
text = errorMessage,
color = Color(0xFFFF8A80),
fontSize = 13.sp
)
}
Button(
onClick = {
onLogin(usuario.trim(), password)
},
modifier = Modifier
.fillMaxWidth()
.height(54.dp),
enabled = puedeIngresar,
shape = RoundedCornerShape(14.dp),
colors = ButtonDefaults.buttonColors(
containerColor = LoginAccentBlue,
disabledContainerColor = LoginAccentBlue.copy(
alpha = 0.4f
)
)
) {
if (isLoading) {
CircularProgressIndicator(
modifier = Modifier.size(22.dp),
color = LoginWhite,
strokeWidth = 2.dp
)
} else {
Text(
text = "Iniciar sesión",
fontSize = 16.sp,
fontWeight = FontWeight.Bold
)
}
}
if (biometricEnabled) {
HorizontalDivider(
color = LoginSoftBlue.copy(alpha = 0.25f)
)
OutlinedButton(
onClick = onBiometricLogin,
modifier = Modifier
.fillMaxWidth()
.height(54.dp),
enabled = !isLoading,
shape = RoundedCornerShape(14.dp),
colors = ButtonDefaults.outlinedButtonColors(
contentColor = LoginWhite
)
) {
Text(
text = "◎",
fontSize = 24.sp
)
Spacer(modifier = Modifier.width(10.dp))
Text(
text = "Ingresar con biometría",
fontWeight = FontWeight.SemiBold
)
}
}
}
}
Spacer(modifier = Modifier.height(26.dp))
Text(
text = "Limpieza que se ve, protección que se siente",
color = LoginSoftBlue,
fontSize = 12.sp
)
Spacer(modifier = Modifier.height(20.dp))
}
}
}
@Composable
private fun ImageLogo() {
androidx.compose.foundation.Image(
painter = painterResource(id = R.drawable.prestige_clean_logo),
contentDescription = "Logo Prestige Clean",
modifier = Modifier
.fillMaxWidth()
.height(145.dp)
.padding(16.dp),
contentScale = androidx.compose.ui.layout.ContentScale.Fit
)
}
@Composable
private fun loginTextFieldColors() =
OutlinedTextFieldDefaults.colors(
focusedTextColor = LoginWhite,
unfocusedTextColor = LoginWhite,
focusedBorderColor = LoginAccentBlue,
unfocusedBorderColor = LoginSoftBlue.copy(alpha = 0.5f),
focusedLabelColor = LoginAccentBlue,
unfocusedLabelColor = LoginSoftBlue,
cursorColor = LoginAccentBlue
)
package com.negocio.desinfectantes.data
import io.github.jan.supabase.auth.user.UserSession
import io.github.jan.supabase.functions.functions
import io.github.jan.supabase.auth.auth
import io.ktor.client.call.body
import kotlinx.coroutines.CancellationException
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import android.util.Log
@OptIn(kotlin.time.ExperimentalTime::class)
class LoginService {
private val supabase = SupabaseClientProvider.client
suspend fun iniciarSesion(
usuario: String,
password: String
): LoginUsuarioResultado {
try {
require(usuario.isNotBlank()) {
"Escribe tu nombre de usuario."
}
require(password.isNotBlank()) {
"Escribe tu contraseña."
}
// Enviar las credenciales a la Edge Function.
val respuesta = supabase.functions.invoke(
function = "login-usuario",
body = buildJsonObject {
put("usuario", usuario.trim().lowercase())
put("password", password)
}
)
// Interpretar la respuesta del servidor.
val resultado = respuesta.body<LoginRespuesta>()
if (
!resultado.ok ||
resultado.session == null ||
resultado.profile == null
) {
throw Exception(
resultado.error ?: "No se pudo iniciar sesión."
)
}
val sesion = resultado.session
// Importar la sesión real en Supabase Auth.
supabase.auth.importSession(
UserSession(
accessToken = sesion.accessToken,
refreshToken = sesion.refreshToken,
expiresIn = sesion.expiresIn,
tokenType = sesion.tokenType,
user = null
)
)
return LoginUsuarioResultado(
id = resultado.profile.id,
nombreCompleto = resultado.profile.nombreCompleto,
rol = resultado.profile.rol
)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.e(
"PrestigeLogin",
"ERROR AL INICIAR SESION",
e
)
throw Exception(
"No se pudo iniciar sesión. Verifica tus datos o tu conexión.",
e
)
}
}
}
@Serializable
private data class LoginRespuesta(
val ok: Boolean = false,
val session: LoginSesion? = null,
val profile: LoginPerfil? = null,
val error: String? = null
)
@Serializable
private data class LoginSesion(
@SerialName("access_token")
val accessToken: String,
@SerialName("refresh_token")
val refreshToken: String,
@SerialName("expires_in")
val expiresIn: Long,
@SerialName("token_type")
val tokenType: String
)
@Serializable
private data class LoginPerfil(
val id: String,
@SerialName("nombre_completo")
val nombreCompleto: String,
val rol: String
)
data class LoginUsuarioResultado(
val id: String,
val nombreCompleto: String,
val rol: String
)
12,812 chars · codepastes.com