-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (76 loc) · 2.89 KB
/
script.js
File metadata and controls
90 lines (76 loc) · 2.89 KB
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
// Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyBt6CaKy5d_fUX5YCsQlEtP4vzT-LPtQl0",
authDomain: "gogreen-ee529.firebaseapp.com",
projectId: "gogreen-ee529",
storageBucket: "gogreen-ee529.appspot.com",
messagingSenderId: "427559388203",
appId: "1:427559388203:web:80af44ead9caff02ff566e",
measurementId: "G-WH0FN2BSG0"
};
// Initialize Firebase with compatibility library
const app = firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const auth = firebase.auth();
// DOM elements
const input = document.querySelector('.input');
const submit = document.getElementById('submit');
// Enhanced save function with reCAPTCHA and error handling
async function saveNameToFirestore() {
const name = input.value.trim();
if (!name) {
alert('Please enter your name');
return;
}
try {
// Get reCAPTCHA token
const token = await grecaptcha.execute('6Ld5OCYrAAAAAH4BjlbdjtOaxrrh2u_vsJpxZHnl', { action: 'submit' });
// Check Firebase connection
await db.collection("test").doc("connection-test").get();
// Save user data along with reCAPTCHA token
await db.collection("users").add({
name: name,
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
recaptchaToken: token // Store the reCAPTCHA token (optional, for validation purposes)
});
// Store name in sessionStorage and redirect
sessionStorage.setItem('name', name);
window.location.href = "index2.html";
} catch (error) {
console.error("Error:", error);
alert(`Error: ${error.message}`);
}
}
// Event listeners with debouncing to prevent multiple submissions
let isSubmitting = false;
submit.addEventListener('click', () => {
if (!isSubmitting) {
isSubmitting = true;
saveNameToFirestore().finally(() => isSubmitting = false);
}
});
window.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && !isSubmitting) {
isSubmitting = true;
saveNameToFirestore().finally(() => isSubmitting = false);
}
});
// const input = document.querySelector('.input');
// const submit = document.getElementById('submit');
// const button = document.querySelector('.hidden');
// function getname() {
// if (input.value.trim() !== '') {
// // Store the name in local storage
// localStorage.setItem('name', input.value.trim());
// window.location.href = "index2.html";
// } else {
// alert('Name is required');
// }
// }
// // Example usage: Attach this function to an input event or button click
// submit.addEventListener('click', getname);
// window.addEventListener('keypress', function (event) {
// if (event.key === 'Enter') {
// getname();
// }
// });