Spaces:
Sleeping
Sleeping
File size: 7,144 Bytes
c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 6896bb9 c79a331 e1344a7 c79a331 6896bb9 e1344a7 6896bb9 e1344a7 c79a331 6896bb9 3254d80 6896bb9 3254d80 6896bb9 3254d80 6896bb9 3254d80 6896bb9 c79a331 3254d80 6896bb9 3254d80 c79a331 6896bb9 c79a331 3254d80 c79a331 3254d80 c79a331 3254d80 6896bb9 c79a331 3254d80 c79a331 6896bb9 c79a331 3254d80 c79a331 3254d80 6896bb9 3254d80 6896bb9 c79a331 3254d80 6896bb9 c79a331 3254d80 c79a331 3254d80 c79a331 6896bb9 3254d80 6896bb9 c79a331 3254d80 0d897c9 c79a331 6896bb9 c79a331 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.1/firebase-app.js";
import {
getAuth,
GoogleAuthProvider,
signInWithPopup,
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
onAuthStateChanged,
signOut
} from "https://www.gstatic.com/firebasejs/9.22.1/firebase-auth.js";
import {
getFirestore,
doc,
getDoc,
setDoc,
updateDoc,
serverTimestamp
} from "https://www.gstatic.com/firebasejs/9.22.1/firebase-firestore.js";
// === Firebase config ===
const firebaseConfig = {
apiKey: "AIzaSyD89ncdY0AqNeYn-kpgdjlyfDKgsfgA1hk",
authDomain: "roomrestyler.firebaseapp.com",
projectId: "roomrestyler",
storageBucket: "roomrestyler.firebasestorage.app",
messagingSenderId: "997195017503",
appId: "1:997195017503:web:6e34afb2c2a5ce0a3e142a",
measurementId: "G-2DL70136H5"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
// === DOM refs ===
const authForm = document.getElementById("authForm");
const userInfo = document.getElementById("userInfo");
const userEmailEl = document.getElementById("userEmail");
const tokenCount = document.getElementById("tokenCount");
const toolbox = document.getElementById("toolbox");
const uploadSection = document.getElementById("uploadSection");
const imageInput = document.getElementById("imageInput");
const preview = document.getElementById("preview");
const result = document.getElementById("result");
const loader = document.getElementById("loader");
// === Google Sign-In ===
async function googleLogin() {
const provider = new GoogleAuthProvider();
try {
const result = await signInWithPopup(auth, provider);
const user = result.user;
const userRef = doc(db, "users", user.uid);
const snap = await getDoc(userRef);
// If first-time Google sign-in, give them tokens
if (!snap.exists()) {
await setDoc(userRef, {
email: user.email,
tokens: 10,
createdAt: serverTimestamp()
});
}
// Update token count immediately
const updatedSnap = await getDoc(userRef);
const tokens = updatedSnap.exists() ? updatedSnap.data().tokens : 0;
tokenCount.innerText = tokens;
alert("Logged in with Google!");
} catch (err) {
console.error(err);
alert("Google sign-in failed: " + err.message);
}
}
// === Email/Password Sign-Up ===
async function signUp() {
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
try {
const cred = await createUserWithEmailAndPassword(auth, email, password);
const userRef = doc(db, "users", cred.user.uid);
await setDoc(userRef, {
email,
tokens: 10,
createdAt: serverTimestamp()
});
// Update token count immediately
tokenCount.innerText = 10;
alert("Signed up successfully! You’ve received 10 free tokens.");
} catch (err) {
console.error(err);
alert("Sign-up failed: " + err.message);
}
}
// === Email/Password Login ===
async function logIn() {
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
try {
const cred = await signInWithEmailAndPassword(auth, email, password);
const userRef = doc(db, "users", cred.user.uid);
const snap = await getDoc(userRef);
// Update token count immediately
const tokens = snap.exists() ? snap.data().tokens : 0;
tokenCount.innerText = tokens;
alert("Logged in successfully!");
} catch (err) {
console.error(err);
alert("Login failed: " + err.message);
}
}
async function logOut() {
try {
await signOut(auth);
alert("Logged out successfully.");
} catch (err) {
console.error(err);
alert("Logout failed: " + err.message);
}
}
// === Auth State Listener ===
onAuthStateChanged(auth, async (user) => {
if (user) {
authForm.style.display = "none";
userInfo.style.display = "flex";
toolbox.style.display = "block";
// Update user email and token count
userEmailEl.innerText = user.email;
const snap = await getDoc(doc(db, "users", user.uid));
const tokens = snap.exists() ? snap.data().tokens : 0;
tokenCount.innerText = tokens;
} else {
authForm.style.display = "flex";
userInfo.style.display = "none";
toolbox.style.display = "none";
}
});
// === Sliders live update ===
document.getElementById("strength").oninput = function () {
document.getElementById("strengthValue").innerText = this.value;
};
document.getElementById("guidanceScale").oninput = function () {
document.getElementById("guidanceValue").innerText = this.value;
};
// === Drag & Drop ===
uploadSection.addEventListener("dragover", (e) => {
e.preventDefault();
uploadSection.style.background = "#e6f4ff";
});
uploadSection.addEventListener("dragleave", () => {
uploadSection.style.background = "";
});
uploadSection.addEventListener("drop", (e) => {
e.preventDefault();
uploadSection.style.background = "";
handleImage(e.dataTransfer.files[0]);
});
imageInput.addEventListener("change", () => handleImage(imageInput.files[0]));
function handleImage(file) {
if (!file) return;
const reader = new FileReader();
reader.onload = (e) =>
(preview.innerHTML = `<img src="${e.target.result}" alt="Preview">`);
reader.readAsDataURL(file);
}
// === Restyle & Deduct Token ===
async function submitForm() {
const user = auth.currentUser;
if (!user) return alert("Please log in first.");
const file = imageInput.files[0];
const prompt = document.getElementById("prompt").value;
if (!file || !prompt) return alert("Upload an image and enter a prompt.");
// Check tokens
const userRef = doc(db, "users", user.uid);
const snap = await getDoc(userRef);
const tokens = snap.exists() ? snap.data().tokens : 0;
if (tokens <= 0) return alert("❌ No credits left!");
// Call backend
const formData = new FormData();
formData.append("image", file);
formData.append("prompt", prompt);
formData.append("negative_prompt", document.getElementById("negativePrompt").value);
formData.append("strength", document.getElementById("strength").value);
formData.append("guidance_scale", document.getElementById("guidanceScale").value);
loader.classList.remove("hidden");
result.innerHTML = "";
try {
const res = await fetch(`${window.location.origin}/restyle`, {
method: "POST",
body: formData,
});
if (!res.ok) throw new Error("Server error");
// Deduct one token
await updateDoc(userRef, { tokens: tokens - 1 });
// Update on-screen count
tokenCount.innerText = tokens - 1;
const blob = await res.blob();
loader.classList.add("hidden");
result.innerHTML = `<h3>Restyled Result:</h3><img src="${URL.createObjectURL(
blob
)}" alt="Result">`;
} catch (err) {
loader.classList.add("hidden");
result.innerHTML = `<p style="color:red">❌ ${err.message}</p>`;
}
}
// Expose functions for inline handlers
window.googleLogin = googleLogin;
window.signUp = signUp;
window.logIn = logIn;
window.logOut = logOut;
window.submitForm = submitForm;
|