-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistro.html
More file actions
174 lines (145 loc) · 5.19 KB
/
registro.html
File metadata and controls
174 lines (145 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Registro - Clínica Dra. Leidi Silva</title>
<style>
body{
font-family: Arial, sans-serif;
background-color:#eef3ff;
display:flex;
justify-content:center;
align-items:center;
height:100vh;
margin:0;
}
.form-container{
background:white;
padding:30px;
border-radius:10px;
box-shadow:0 4px 12px rgba(0,0,0,0.1);
text-align:center;
width:340px;
}
h2{ margin:0 0 14px; }
input, select{
width:100%;
padding:10px;
margin:8px 0;
border:1px solid #ccc;
border-radius:5px;
box-sizing:border-box;
}
button{
width:100%;
padding:10px;
background-color:#2c52ff;
color:white;
border:none;
border-radius:5px;
cursor:pointer;
font-weight:bold;
margin-top:6px;
}
button:hover{ background-color:#1a38cc; }
.mensaje-error{ margin-top:10px; color:red; white-space:pre-line; }
.mensaje-info{ margin-top:10px; color:#6b7280; font-size:12px; }
a{ color:#4b0082; }
</style>
</head>
<body>
<div class="form-container">
<h2>Software Dental<br/>Clínica Dra. Leidi Silva</h2>
<input type="email" id="email" placeholder="Correo electrónico" required />
<input type="password" id="password" placeholder="Contraseña" required />
<!-- En UI puedes mostrar con mayúscula, pero guardaremos en minúscula -->
<select id="role" required>
<option value="user">Usuario</option>
<option value="doctor">Doctor</option>
<option value="auxiliar">Auxiliar</option>
<option value="admin">Administrador</option>
</select>
<button id="boton-registrar">Crear cuenta</button>
<p style="margin-top:12px;">¿Ya tienes cuenta? <a href="login.html">Inicia sesión</a></p>
<p class="mensaje-info">⚠️ Esta página solo está habilitada para el administrador.</p>
<p id="error" class="mensaje-error"></p>
</div>
<script type="module">
import { createClient } from "https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm";
const supabaseUrl = "https://dmcfrmpqlkrqbpjhrfyo.supabase.co";
const supabaseKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImRtY2ZybXBxbGtycWJwamhyZnlvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjI5MzU0MDIsImV4cCI6MjA3ODUxMTQwMn0.YY_6t_lCTs5mK-a_eEdBFa6l9_NBM3B4YDtWOXayCBE";
const supabase = createClient(supabaseUrl, supabaseKey);
const ADMIN_EMAIL = "camilo-1000@hotmail.com";
const LOGIN_URL = "login.html";
const INDEX_URL = "index.html";
function setMsg(txt){
const errorDiv = document.getElementById("error");
errorDiv.textContent = txt || "";
}
async function protegerRegistro(){
const { data: { session }, error } = await supabase.auth.getSession();
if (error) {
alert("Error validando sesión: " + error.message);
window.location.href = LOGIN_URL;
return;
}
if (!session) {
alert("Debes iniciar sesión.");
window.location.href = LOGIN_URL;
return;
}
const email = (session.user?.email || "").toLowerCase();
if (email !== ADMIN_EMAIL.toLowerCase()) {
alert("No autorizado.");
window.location.href = INDEX_URL;
return;
}
}
await protegerRegistro();
document.getElementById("boton-registrar").addEventListener("click", async () => {
setMsg("");
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value.trim();
const role = (document.getElementById("role").value || "").toLowerCase();
if (!email || !password || !role) {
setMsg("Por favor completa todos los campos.");
return;
}
try {
// 1) Crear usuario en Auth (esto envía correo de confirmación si lo tienes activo)
const { data, error } = await supabase.auth.signUp({ email, password });
if (error) {
setMsg("Error creando usuario: " + error.message);
return;
}
const newUserId = data.user?.id;
if (!newUserId) {
setMsg("Usuario creado, pero no se pudo obtener el ID.");
return;
}
// 2) Asignar rol en profiles (UPDATE, porque normalmente el profile ya existe por trigger con role='user')
const { error: uerr } = await supabase
.from("profiles")
.update({ role, email })
.eq("id", newUserId);
if (uerr) {
setMsg(
"✅ Usuario creado.\n" +
"⚠️ Pero no se pudo asignar el rol (profiles).\n\n" +
"Causa probable: RLS.\n" +
"Solución: crea la policy de admin para UPDATE en profiles.\n\n" +
"Detalle: " + uerr.message
);
return;
}
alert("✅ Cuenta creada y rol asignado.\n(Revisa el correo para confirmar si aplica).");
window.location.href = INDEX_URL;
} catch (err) {
console.error(err);
setMsg("Error inesperado al conectar con Supabase.");
}
});
</script>
</body>
</html>