Faili ya asili inayofanya kazi
public/app.js
Majina katika msimbo na maandishi ya kurasa za mfano zinazofanya kazi yamehifadhiwa kama yalivyo katika asili. Somo na maelekezo ya faili yanayafafanua kwa Kiswahili.
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("#load-note").addEventListener("click", async () => {
const out = document.querySelector("#note");
out.textContent = "";
if (!client) return;
const {
data: { session },
} = await client.auth.getSession();
if (!session) {
out.textContent = "Sign in first.";
return;
}
try {
const r = await fetch("/.netlify/functions/mini-private-note", {
headers: { Authorization: "Bearer " + session.access_token },
});
if (!r.ok) {
out.textContent =
r.status === 401
? "Your session was not accepted."
: "The note is currently unavailable.";
return;
}
const value = await r.json();
out.textContent = value.note;
} catch {
out.textContent = "Could not load the note.";
}
});
if (client)
client.auth.onAuthStateChange((event, session) => {
if (!session) document.querySelector("#note").textContent = "";
});