Subira ku isomo

Dosiye y'umwimerere ikora

public/app.js

Kode n'impapuro z'ingero zikora bigumana amazina n'inyandiko by'umwimerere. Isomo n'amabwiriza ya dosiye birabisobanura mu rurimi rwawe.

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("#upload").addEventListener("submit", async (e) => {
  e.preventDefault();
  const status = document.querySelector("#upload-status"),
    f = e.target.elements.picture.files[0];
  if (!client) return;
  const {
    data: { user },
  } = await client.auth.getUser();
  if (!user) {
    status.textContent = "Sign in first.";
    return;
  }
  const types = { "image/png": "png", "image/jpeg": "jpg", "image/webp": "webp" };
  if (!f || !types[f.type] || f.size > 2097152) {
    status.textContent = "Choose a PNG/JPEG/WebP no larger than 2MB.";
    return;
  }
  const objectPath = user.id + "/" + crypto.randomUUID() + "." + types[f.type];
  const { error } = await client.storage
    .from("avatars")
    .upload(objectPath, f, { contentType: f.type, upsert: false });
  if (error) {
    status.textContent = "Upload was not accepted.";
    return;
  }
  const { data, error: readError } = await client.storage
    .from("avatars")
    .createSignedUrl(objectPath, 60);
  if (readError) {
    status.textContent = "Uploaded, but preview access failed.";
    return;
  }
  document.querySelector("#preview").src = data.signedUrl;
  status.textContent = "Stored at " + objectPath + ". This preview link expires.";
});
if (client)
  client.auth.onAuthStateChange((event, session) => {
    if (!session) document.querySelector("#preview").removeAttribute("src");
  });