Archivo funcional original
reference/public/app.js
El código y las páginas de ejemplo funcionales conservan sus identificadores y textos de muestra originales. La lección y las instrucciones de los archivos los explican en tu idioma.
import { client } from "./client.js";
const status = document.querySelector("[role=status]"),
email = document.querySelector("[name=email]"),
token = document.querySelector("[name=token]");
function show(message) {
status.textContent = message;
}
if (!client)
show("Configure config.js with your practice project URL and publishable key.");
document.querySelector("#request").addEventListener("click", async () => {
if (!client) return;
const { error } = await client.auth.signInWithOtp({
email: email.value.trim(),
options: { shouldCreateUser: true },
});
show(
error
? "Could not request a code. Check your project settings."
: "Request accepted. Check your inbox for the sign-in code.",
);
});
document.querySelector("#verify").addEventListener("click", async () => {
if (!client) return;
const { data, error } = await client.auth.verifyOtp({
email: email.value.trim(),
token: token.value.trim(),
type: "email",
});
show(error || !data.session ? "Code was not accepted." : "Signed in.");
});
document.querySelector("#signout").addEventListener("click", async () => {
if (!client) return;
const { error } = await client.auth.signOut();
show(error ? "Sign-out failed." : "Signed out of this browser.");
});
if (client)
client.auth.onAuthStateChange((event, session) => {
document.querySelector("#identity").textContent = session
? "Signed-in account: " + session.user.email
: "No signed-in account.";
});
document.querySelector("#profile").addEventListener("submit", async (e) => {
e.preventDefault();
const out = document.querySelector("#profile-status");
if (!client) return;
const {
data: { user },
error: authError,
} = await client.auth.getUser();
if (authError || !user) {
out.textContent = "Sign in first.";
return;
}
const name = e.target.elements.display_name.value.trim();
if (!name || name.length > 80) {
out.textContent = "Use a display name from 1 to 80 characters.";
return;
}
const bio = e.target.elements.bio.value.trim();
if (bio.length > 300) {
out.textContent = "Bio must be at most 300 characters.";
return;
}
const { data, error } = await client
.from("profiles")
.upsert({ id: user.id, display_name: name, bio })
.select("display_name,bio")
.single();
out.textContent = error
? "Profile was not saved."
: "Saved display name: " + data.display_name + "; bio: " + data.bio;
});
document.querySelector("#load-profile").addEventListener("click", async () => {
if (!client) return;
const {
data: { user },
} = await client.auth.getUser();
if (!user) return;
const { data, error } = await client
.from("profiles")
.select("display_name,bio")
.eq("id", user.id)
.maybeSingle();
document.querySelector("#profile-status").textContent = error
? "Could not read the profile."
: data
? "Stored display name: " + data.display_name + "; bio: " + data.bio
: "No profile has been created yet.";
});