Arquivo funcional original
public/app.js
O código e as páginas funcionais de exemplo mantêm seus identificadores e textos originais. A lição e as orientações dos arquivos explicam esses elementos no seu 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.";
});