@@ -18,52 +18,56 @@ import {
1818 getDownloadURL
1919} from "firebase/storage" ;
2020
21+ // Firebase initialization (you must already have this in your main project setup)
2122const auth = getAuth ( ) ;
2223const db = getFirestore ( ) ;
2324const storage = getStorage ( ) ;
2425
2526let userEmail = null ;
2627
27- // Auth check
28+ // ✅ AUTH CHECK
2829onAuthStateChanged ( auth , ( user ) => {
2930 if ( user ) {
3031 userEmail = user . email ;
3132 } else {
3233 console . log ( "No user is signed in" ) ;
34+ // Redirect to login with return URL (FULL URL for better redirect)
3335 window . location . href = `/login/?redirect_url=${ encodeURIComponent ( window . location . href ) } ` ;
3436 }
3537} ) ;
3638
39+ // ✅ UPLOAD CV TO STORAGE
3740async function uploadCV ( file ) {
38- const fileName = ` ${ userEmail . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, "_" ) } _ ${ file . name } ` ;
39- const cvRef = ref ( storage , " user_cv/" + fileName ) ;
40- await uploadBytes ( cvRef , file ) ;
41- return await getDownloadURL ( cvRef ) ;
41+ const sanitizedFileName = file . name . replace ( / [ ^ a - z A - Z 0 - 9 _ . - ] / g, "_" ) ;
42+ const storageRef = ref ( storage , ` user_cv/${ userEmail } _ ${ sanitizedFileName } ` ) ;
43+ await uploadBytes ( storageRef , file ) ;
44+ return await getDownloadURL ( storageRef ) ;
4245}
4346
47+ // ✅ SAVE CV URL TO FIRESTORE
4448async function saveCVToDatabase ( ) {
4549 const uploadButton = document . getElementById ( "btn" ) ;
46- uploadButton . innerHTML = "Uploading..." ;
47- uploadButton . disabled = true ;
50+ const fileInput = document . getElementById ( "cv" ) ;
51+ const file = fileInput . files [ 0 ] ;
4852
49- const cvFile = document . getElementById ( "cv" ) . files [ 0 ] ;
50- if ( ! cvFile ) {
53+ if ( ! file ) {
5154 Toastify ( { text : "Please select a file" , duration : 3000 , style : { background : "red" } } ) . showToast ( ) ;
52- uploadButton . innerHTML = "UPLOAD RESUME" ;
53- uploadButton . disabled = false ;
5455 return ;
5556 }
5657
58+ uploadButton . disabled = true ;
59+ uploadButton . innerText = "Uploading..." ;
60+
5761 try {
58- const cvUrl = await uploadCV ( cvFile ) ;
59- const userProfileRef = doc ( db , "user_profile" , userEmail ) ;
62+ const cvUrl = await uploadCV ( file ) ;
63+ const userRef = doc ( db , "user_profile" , userEmail ) ;
6064
61- const docSnap = await getDoc ( userProfileRef ) ;
62- if ( ! docSnap . exists ( ) ) {
63- await setDoc ( userProfileRef , { about : { } } ) ;
65+ const userSnap = await getDoc ( userRef ) ;
66+ if ( ! userSnap . exists ( ) ) {
67+ await setDoc ( userRef , { about : { } } ) ;
6468 }
6569
66- await updateDoc ( userProfileRef , {
70+ await updateDoc ( userRef , {
6771 "about.cv" : cvUrl
6872 } ) ;
6973
@@ -83,13 +87,14 @@ async function saveCVToDatabase() {
8387 window . location . href = "/myaccount/" ;
8488 } , 3000 ) ;
8589
86- } catch ( error ) {
87- console . error ( "Upload error :" , error ) ;
90+ } catch ( err ) {
91+ console . error ( "Upload failed :" , err ) ;
8892 Toastify ( { text : "Upload failed" , duration : 3000 , style : { background : "red" } } ) . showToast ( ) ;
8993 }
9094
91- uploadButton . innerHTML = "UPLOAD RESUME" ;
9295 uploadButton . disabled = false ;
96+ uploadButton . innerText = "UPLOAD RESUME" ;
9397}
9498
99+ // ✅ HANDLE BUTTON CLICK
95100document . getElementById ( "btn" ) . addEventListener ( "click" , saveCVToDatabase ) ;
0 commit comments