-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
217 lines (189 loc) · 6.92 KB
/
Copy pathscript.js
File metadata and controls
217 lines (189 loc) · 6.92 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
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
const firebaseConfig = {
apiKey: "AIzaSyAnenmOuraTsXxgRczYqOq7rwOeAKHTC1w",
authDomain: "online-voting-5e6d4.firebaseapp.com",
projectId: "online-voting-5e6d4",
storageBucket: "online-voting-5e6d4.appspot.com",
messagingSenderId: "389538297568",
appId: "1:389538297568:web:0d3a66a9ef09942b50c285",
measurementId: "G-DTLH3MC89E"
};
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.firestore();
const storage = firebase.storage();
const validAadhaars = ["123456789012", "987654321098", "987654321099"]; // Add more Aadhaar numbers as needed
const validVoterIds = ["ABC1234567", "XYZ9876543", "XYZ9876546"]; // Add more Voter IDs as needed
function hashData(data) {
return CryptoJS.SHA256(data).toString();
}
function validateAadhaar(aadhaar) {
return validAadhaars.includes(aadhaar);
}
function validateVoterId(voterId) {
return validVoterIds.includes(voterId);
}
document.getElementById('nextBtn1').addEventListener('click', () => {
if (validatePhase1()) {
document.getElementById('phase1Form').classList.remove('active');
document.getElementById('phase2Form').classList.add('active');
document.getElementById('progressBar').style.width = '66%';
}
});
document.getElementById('nextBtn2').addEventListener('click', async () => {
const isUnique = await validateEmailUniqueness();
if (isUnique && validatePhase2()) {
const email = document.getElementById('email').value;
const phone = document.getElementById('phone').value;
// Store temporary data in a separate collection
db.collection('tempSignUpData').doc('temp_' + email).set({
email: email,
phone: phone,
createdAt: firebase.firestore.FieldValue.serverTimestamp()
})
.then(() => {
document.getElementById('phase2Form').classList.remove('active');
document.getElementById('phase3Form').classList.add('active');
document.getElementById('progressBar').style.width = '100%';
})
.catch((error) => {
console.error('Error storing temporary data:', error);
displayError('emailFeedback', 'There was an error processing your request. Please try again.');
});
}
});
document.getElementById('phase3Form').addEventListener('submit', (e) => {
e.preventDefault();
if (validatePhase3()) {
const email = document.getElementById('email').value;
const phone = document.getElementById('phone').value;
const aadhaar = document.getElementById('aadhaar').value;
const voterId = document.getElementById('voterId').value;
const name = document.getElementById('name').value;
const age = document.getElementById('age').value;
const sex = document.getElementById('sex').value;
const aadhaarHash = hashData(aadhaar);
const voterIdHash = hashData(voterId);
auth.createUserWithEmailAndPassword(email, 'defaultPassword')
.then((userCredential) => {
const user = userCredential.user;
// Move data to the main collection and delete the temporary record
return db.collection('users').doc(user.uid).set({
email: user.email,
phone: phone,
aadhaarHash: aadhaarHash,
voterIdHash: voterIdHash,
name: name,
age: age,
sex: sex,
createdAt: firebase.firestore.FieldValue.serverTimestamp()
})
.then(() => {
// Optionally delete the temporary data
return db.collection('tempSignUpData').doc('temp_' + email).delete();
});
})
.then(() => {
return storage.ref('ids/' + auth.currentUser.uid).putString(`Aadhaar: ${aadhaarHash}, VoterID: ${voterIdHash}`);
})
.then(() => {
document.getElementById('phase3Form').classList.remove('active');
document.getElementById('successMessage').classList.add('active');
setTimeout(() => {
window.location.href = 'user-session.html';
}, 2000);
})
.catch((error) => {
console.error('Error signing up:', error.message);
displayError('aadhaarFeedback', error.message);
});
}
});
async function validateEmailUniqueness() {
const email = document.getElementById('email').value;
try {
const querySnapshot = await db.collection('users').where('email', '==', email).get();
if (!querySnapshot.empty) {
displayError('emailFeedback', 'Email already in use');
return false;
} else {
displaySuccess('emailFeedback', 'Valid email');
return true;
}
} catch (error) {
console.error('Error checking email:', error);
return false;
}
}
function validatePhase1() {
const name = document.getElementById('name').value;
const age = document.getElementById('age').value;
const sex = document.getElementById('sex').value;
let valid = true;
if (name.trim().length > 0) {
displaySuccess('nameFeedback', 'Valid name');
} else {
displayError('nameFeedback', 'Name is required');
valid = false;
}
if (age >= 18) {
displaySuccess('ageFeedback', 'Valid age');
} else {
displayError('ageFeedback', 'Must be at least 18 years old');
valid = false;
}
if (sex) {
displaySuccess('sexFeedback', 'Sex selected');
} else {
displayError('sexFeedback', 'Sex is required');
valid = false;
}
return valid;
}
function validatePhase2() {
const email = document.getElementById('email').value;
const phone = document.getElementById('phone').value;
let valid = true;
if (email.includes('@')) {
displaySuccess('emailFeedback', 'Valid email');
} else {
displayError('emailFeedback', 'Invalid email');
valid = false;
}
if (phone.match(/^\d{10}$/)) {
displaySuccess('phoneFeedback', 'Valid phone number');
} else {
displayError('phoneFeedback', 'Invalid phone number');
valid = false;
}
return valid;
}
function validatePhase3() {
const aadhaar = document.getElementById('aadhaar').value;
const voterId = document.getElementById('voterId').value;
let valid = true;
if (validateAadhaar(aadhaar)) {
displaySuccess('aadhaarFeedback', 'Valid Aadhaar number');
} else {
displayError('aadhaarFeedback', 'Invalid Aadhaar number');
valid = false;
}
if (validateVoterId(voterId)) {
displaySuccess('voterIdFeedback', 'Valid Voter ID');
} else {
displayError('voterIdFeedback', 'Invalid Voter ID');
valid = false;
}
return valid;
}
function displayError(elementId, message) {
const element = document.getElementById(elementId);
element.textContent = message;
element.classList.remove('valid-feedback');
element.classList.add('invalid-feedback');
}
function displaySuccess(elementId, message) {
const element = document.getElementById(elementId);
element.textContent = message;
element.classList.remove('invalid-feedback');
element.classList.add('valid-feedback');
}