|
1 | | -let userEmail; |
2 | | -// Check if the user is signed in |
3 | | -auth.onAuthStateChanged((user) => { |
4 | | - if (user) { |
5 | | - userEmail = user.email; |
6 | | - } else { |
7 | | - // No user is signed in |
8 | | - console.log("No user is signed in"); |
9 | | - window.location.href = `/login/?redirect_url=${encodeURIComponent(window.location.href)}`; |
| 1 | +import { |
| 2 | + getAuth, |
| 3 | + onAuthStateChanged |
| 4 | +} from "firebase/auth"; |
10 | 5 |
|
11 | | - } |
12 | | -}); |
| 6 | +import { |
| 7 | + getFirestore, |
| 8 | + doc, |
| 9 | + getDoc, |
| 10 | + setDoc, |
| 11 | + updateDoc |
| 12 | +} from "firebase/firestore"; |
13 | 13 |
|
14 | | -const email = localStorage.getItem("email"); |
| 14 | +import { |
| 15 | + getStorage, |
| 16 | + ref, |
| 17 | + uploadBytes, |
| 18 | + getDownloadURL |
| 19 | +} from "firebase/storage"; |
15 | 20 |
|
16 | | -let cvUrl = ""; |
| 21 | +// Firebase initialization (you must already have this in your main project setup) |
| 22 | +const auth = getAuth(); |
| 23 | +const db = getFirestore(); |
| 24 | +const storage = getStorage(); |
17 | 25 |
|
18 | | -async function uploadCV(file) { |
19 | | - const cvRef = ref(storage, "user_cv/" + file.name); |
20 | | - await uploadBytes(cvRef, file); |
| 26 | +let userEmail = null; |
| 27 | + |
| 28 | +// ✅ AUTH CHECK |
| 29 | +onAuthStateChanged(auth, (user) => { |
| 30 | + if (user) { |
| 31 | + userEmail = user.email; |
| 32 | + } else { |
| 33 | + console.log("No user is signed in"); |
| 34 | + // Redirect to login with full return URL |
| 35 | + window.location.href = `/login/?redirect_url=${encodeURIComponent(window.location.href)}`; |
| 36 | + } |
| 37 | +}); |
21 | 38 |
|
22 | | - const url = await getDownloadURL(cvRef); |
23 | | - return url; // Return empty string if URL is undefined |
| 39 | +// ✅ UPLOAD CV TO STORAGE |
| 40 | +async function uploadCV(file) { |
| 41 | + const sanitizedFileName = file.name.replace(/[^a-zA-Z0-9_.-]/g, "_"); |
| 42 | + const storageRef = ref(storage, `user_cv/${userEmail}_${sanitizedFileName}`); |
| 43 | + await uploadBytes(storageRef, file); |
| 44 | + return await getDownloadURL(storageRef); |
24 | 45 | } |
25 | 46 |
|
| 47 | +// ✅ SAVE CV URL TO FIRESTORE |
26 | 48 | async function saveCVToDatabase() { |
27 | | - const uploadButton = document.getElementById("btn"); |
28 | | - uploadButton.innerHTML = "Uploading..."; |
29 | | - uploadButton.disabled = true; |
30 | | - const cvFile = document.getElementById("cv").files[0]; |
31 | | - if (cvFile) { |
32 | | - cvUrl = await uploadCV(cvFile); |
33 | | - |
34 | | - const userProfileRef = doc(db, "user_profile", userEmail); |
35 | | - |
36 | | - // Ensure the user profile document exists before updating it |
37 | | - const docSnap = await getDoc(userProfileRef); |
38 | | - if (!docSnap.exists()) { |
39 | | - await setDoc(userProfileRef, { "about": {} }); |
40 | | - } |
41 | | - |
42 | | - await updateDoc(userProfileRef, { "about.cv": cvUrl }) |
43 | | - .then(() => { |
44 | | - Toastify({ |
45 | | - text: "CV Successfully Submitted", |
46 | | - duration: 3000, |
47 | | - newWindow: true, |
48 | | - close: true, |
49 | | - gravity: "top", |
50 | | - position: "right", |
51 | | - stopOnFocus: true, |
52 | | - style: { |
53 | | - background: "linear-gradient(to right, #0d6efd, #586ba6)", |
54 | | - borderRadius: "10px" |
55 | | - } |
56 | | - }).showToast(); |
57 | | - setTimeout(() => { |
58 | | - window.location.href = "/myaccount/"; |
59 | | - }, 3000); |
60 | | - }) |
61 | | - .catch((error) => { |
62 | | - console.error("Error writing document: ", error); |
63 | | - }); |
| 49 | + const uploadButton = document.getElementById("btn"); |
| 50 | + const fileInput = document.getElementById("cv"); |
| 51 | + const file = fileInput.files[0]; |
| 52 | + |
| 53 | + if (!file) { |
| 54 | + Toastify({ |
| 55 | + text: "Please select a file", |
| 56 | + duration: 3000, |
| 57 | + style: { background: "red" } |
| 58 | + }).showToast(); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + uploadButton.disabled = true; |
| 63 | + uploadButton.innerText = "Uploading..."; |
| 64 | + |
| 65 | + try { |
| 66 | + const cvUrl = await uploadCV(file); |
| 67 | + const userRef = doc(db, "user_profile", userEmail); |
| 68 | + |
| 69 | + const userSnap = await getDoc(userRef); |
| 70 | + if (!userSnap.exists()) { |
| 71 | + await setDoc(userRef, { about: {} }); |
64 | 72 | } |
65 | | - |
66 | | - uploadButton.innerHTML = "UPLOAD RESUME"; |
67 | | - uploadButton.disabled = false; |
| 73 | + |
| 74 | + await updateDoc(userRef, { |
| 75 | + "about.cv": cvUrl |
| 76 | + }); |
| 77 | + |
| 78 | + Toastify({ |
| 79 | + text: "CV Successfully Submitted", |
| 80 | + duration: 3000, |
| 81 | + close: true, |
| 82 | + gravity: "top", |
| 83 | + position: "right", |
| 84 | + style: { |
| 85 | + background: "linear-gradient(to right, #0d6efd, #586ba6)", |
| 86 | + borderRadius: "10px" |
| 87 | + } |
| 88 | + }).showToast(); |
| 89 | + |
| 90 | + setTimeout(() => { |
| 91 | + window.location.href = "/myaccount/"; |
| 92 | + }, 3000); |
| 93 | + |
| 94 | + } catch (err) { |
| 95 | + console.error("Upload failed:", err); |
| 96 | + Toastify({ |
| 97 | + text: "Upload failed", |
| 98 | + duration: 3000, |
| 99 | + style: { background: "red" } |
| 100 | + }).showToast(); |
| 101 | + } |
| 102 | + |
| 103 | + uploadButton.disabled = false; |
| 104 | + uploadButton.innerText = "UPLOAD RESUME"; |
68 | 105 | } |
69 | 106 |
|
70 | | -$("#btn").on("click", function () { |
71 | | - saveCVToDatabase(); |
72 | | -}); |
| 107 | +// ✅ HANDLE BUTTON CLICK |
| 108 | +document.getElementById("btn").addEventListener("click", saveCVToDatabase); |
0 commit comments