የመጀመሪያው የሚሠራ ፋይል
reference/public/app.js
ኮዱ እና የሚሠሩ ምሳሌ ገጾች የመጀመሪያ መለያዎቻቸውንና የምሳሌ ጽሑፋቸውን ይይዛሉ። ትምህርቱ እና የፋይል መመሪያዎች በእርስዎ ቋንቋ ያብራሯቸዋል።
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");
});
document.querySelector("#delete-image").addEventListener("click", async () => {
if (!client) return;
const out = document.querySelector("#upload-status"),
{
data: { user },
error: authError,
} = await client.auth.getUser();
if (authError || !user) {
out.textContent = "Sign in first.";
return;
}
const objectPath = document.querySelector("#delete-path").value.trim();
if (!objectPath.startsWith(user.id + "/")) {
out.textContent = "Use an exact object path in your own account folder.";
return;
}
const { error } = await client.storage.from("avatars").remove([objectPath]);
out.textContent = error
? "Delete request failed."
: "Delete request completed. Verify that the exact object is absent; also verify other owners retain their objects.";
});