File size: 12,454 Bytes
f5dadbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06a11c9
f5dadbf
 
 
 
 
06a11c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5dadbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>E. Coli Detection Portal</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }

        #background-video {
            position: fixed;
            right: 0;
            bottom: 0;
            min-width: 100%;
            min-height: 100%;
            width: auto;
            height: auto;
            z-index: -1000;
            background-size: cover;}
        .form-card {
            backdrop-filter: blur(10px);
            background-color: rgba(255, 255, 255, 0.8);
        }
        .toggle-btn {
            transition: all 0.3s ease;
        }
        .toggle-btn:hover {
            transform: scale(1.05);
        }
    </style>
</head>
<body class="min-h-screen flex items-center justify-center p-4">
    <video autoplay loop muted id="background-video">
        <source src="{{ url_for('static', filename='Login.mp4') }}" type="video/mp4">
        Your browser does not support the video tag.
    </video>
    
    <div class="container mx-auto max-w-md">
        <div class="form-card rounded-xl shadow-2xl p-6">
            <div class="text-center mb-6">
                <h1 class="text-3xl font-bold text-gray-800">E. Coli Detection Portal</h1>
                <p class="text-gray-600 mt-2">Secure User Management System</p>
            </div>

            <!-- Toggle Buttons -->
            <div class="flex justify-center space-x-4 mb-6">
                <button id="userModeBtn" class="toggle-btn px-4 py-2 bg-blue-500 text-white rounded-lg focus:outline-none">
                    User Mode
                </button>
                <button id="govtModeBtn" class="toggle-btn px-4 py-2 bg-green-500 text-white rounded-lg focus:outline-none">
                    Govt Mode
                </button>
            </div>

            <!-- User Registration/Login Form -->
            <div id="userSection" class="space-y-4">
                <div id="registrationForm">
                    <h2 class="text-xl font-semibold text-center mb-4">User Registration</h2>
                    <input type="text" id="name" placeholder="Full Name" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-blue-300">
                    <input type="tel" id="phone" placeholder="Phone Number" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-blue-300">
                    <input type="text" id="state" placeholder="State" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-blue-300">
                    <input type="text" id="town" placeholder="Town/City" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-blue-300">
                    <input type="text" id="pincode" placeholder="Pincode" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-blue-300">
                    <input type="password" id="password" placeholder="Set Password" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-blue-300">
                    <button onclick="registerUser()" class="w-full bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition duration-300">
                        Register
                    </button>
                    <p class="text-center mt-3 text-sm text-gray-600">
                      <a href="#" id="switchToLogin" class="text-blue-600 hover:underline">Already have an account? Login</a>
                  </p>
                </div>

                <div id="loginForm" class="hidden">
                    <h2 class="text-xl font-semibold text-center mb-4">User Login</h2>
                    <input type="tel" id="loginPhone" placeholder="Phone Number" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-blue-300">
                    <input type="password" id="loginPassword" placeholder="Password" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-blue-300">
                    <button onclick="userLogin()" class="w-full bg-green-600 text-white p-3 rounded-lg hover:bg-green-700 transition duration-300">
                        Login
                    </button>
                    <p class="text-center mt-3 text-sm text-gray-600">
                        <a href="#" id="switchToRegister" class="text-blue-600 hover:underline">Need an account? Register</a>
                    </p>
                </div>
            </div>

            <!-- Government Login Form -->
            <div id="govtSection" class="hidden space-y-4">
                <h2 class="text-xl font-semibold text-center mb-4">Government Authority Portal</h2>
                <input type="text" id="govtUsername" placeholder="Username" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-green-300">
                <input type="password" id="govtPassword" placeholder="Password" class="w-full p-3 border rounded-lg mb-3 focus:ring-2 focus:ring-green-300">
                <button onclick="govtLogin()" class="w-full bg-green-600 text-white p-3 rounded-lg hover:bg-green-700 transition duration-300">
                    Login
                </button>
                <div id="userHistoryContainer" class="mt-4 max-h-60 overflow-y-auto bg-gray-100 p-4 rounded-lg">
                    <div id="history"></div>
                </div>
            </div>
        </div>
    </div>

    <script type="module">
        import { initializeApp } from "https://www.gstatic.com/firebasejs/11.0.2/firebase-app.js";
        import { getFirestore, doc, setDoc, getDoc, collection, getDocs, query, where } from "https://www.gstatic.com/firebasejs/11.0.2/firebase-firestore.js";

        const firebaseConfig = {
            apiKey: "AIzaSyBVazIeGCASyfCka3nU_INuZytrTdSVmXo",
            authDomain: "snippetscript-37175.firebaseapp.com",
            projectId: "snippetscript-37175",
            storageBucket: "snippetscript-37175.appspot.com",
            messagingSenderId: "154274113551",
            appId: "1:154274113551:web:4e9582f3a24f7d12695b7a",
            measurementId: "G-PSR30H9GH6"
        };

        // Initialize Firebase
        const app = initializeApp(firebaseConfig);
        const db = getFirestore(app);

        // Mode Toggle Functionality
        document.getElementById('userModeBtn').addEventListener('click', () => {
            document.getElementById('userSection').classList.remove('hidden');
            document.getElementById('govtSection').classList.add('hidden');
        });

        document.getElementById('govtModeBtn').addEventListener('click', () => {
            document.getElementById('userSection').classList.add('hidden');
            document.getElementById('govtSection').classList.remove('hidden');
        });

        // Switch between Login and Register
        document.getElementById('switchToRegister').addEventListener('click', (e) => {
            e.preventDefault();
            document.getElementById('registrationForm').classList.remove('hidden');
            document.getElementById('loginForm').classList.add('hidden');
        });

        // Switch between Register and Login
        document.getElementById('switchToLogin').addEventListener('click', (e) => {
            e.preventDefault();
            document.getElementById('loginForm').classList.remove('hidden');
            document.getElementById('registrationForm').classList.add('hidden');
        });
// Register User
async function registerUser() {
    const name = document.getElementById("name").value;
    const phone = document.getElementById("phone").value;
    const state = document.getElementById("state").value;
    const town = document.getElementById("town").value;
    const pincode = document.getElementById("pincode").value;
    const password = document.getElementById("password").value;

    // Enhanced Validation
    if (!name || !phone || !state || !town || !pincode || !password) {
        alert("Please fill in all fields!");
        return;
    }

    // Validate phone number (10 digits)
    if (!/^\d{10}$/.test(phone)) {
        alert("Please enter a valid 10-digit phone number!");
        return;
    }

    // Validate password strength
    if (password.length < 4) {
        alert("Password must be at least 6 characters long!");
        return;
    }

    try {
                // Check if user already exists
                const existingUserQuery = await getDocs(query(collection(db, "users"), where("phone", "==", phone)));
                if (!existingUserQuery.empty) {
                    alert("Phone number already registered!");
                    return;
                }

                // Register User (save to Firestore)
                await setDoc(doc(db, "users", phone), {
                    name: name,
                    phone: phone,
                    state: state,
                    town: town,
                    pincode: pincode,
                    password: password,
                    registeredAt: new Date().toISOString()
                });
        
        // Show success message first
        alert("User registered successfully!");
        
        // Store phone number in localStorage
        localStorage.setItem('userPhone', phone);
        
        // Redirect to user dashboard after a short delay
        setTimeout(() => {
            window.location.href = 'user-dashboard.html';
        }, 1000);
    } catch (error) {
        alert("Error registering user: " + error.message);
    }
}

// User Login
async function userLogin() {
    const phone = document.getElementById("loginPhone").value;
    const password = document.getElementById("loginPassword").value;

    // Validate inputs
    if (!phone || !password) {
        alert("Please enter both phone number and password!");
        return;
    }

    try {
        // Query to find user by phone number
        const userQuery = query(collection(db, "users"), where("phone", "==", phone));
        const querySnapshot = await getDocs(userQuery);

        if (!querySnapshot.empty) {
            // User found, verify password
            const userData = querySnapshot.docs[0].data();
            
            if (userData.password === password) {
                // Show success message first
                alert("Login successful!");
                
                // Store phone number in localStorage for session tracking
                localStorage.setItem('userPhone', phone);
                
                // Redirect to user dashboard after a short delay
                setTimeout(() => {
                    window.location.href = 'user-dashboard.html';
                }, 1000);
            } else {
                // Incorrect password
                alert("Invalid phone number or password!");
            }
        } else {
            // No user found with this phone number
            alert("Invalid phone number or password!");
        }
    } catch (error) {
        alert("Error logging in: " + error.message);
    }
}
        // Government Login
async function govtLogin() {
    const username = document.getElementById("govtUsername").value;
    const password = document.getElementById("govtPassword").value;

    if (username === "govt" && password === "password") {
        const userCollection = await getDocs(collection(db, "users"));
        const historyDiv = document.getElementById("history");
        historyDiv.innerHTML = "<h3 class='text-lg font-bold mb-2'>Registered Users</h3>";
        userCollection.forEach(doc => {
            const data = doc.data();
            historyDiv.innerHTML += `
                <div class="bg-white p-3 rounded-lg shadow-sm mb-2">
                    <p class="font-medium">${data.name}</p>
                    <p class="text-sm text-gray-600">Phone: ${data.phone} | State: ${data.state}</p>
                </div>
            `;
        });

        // Redirect to admin.html after successful login
        window.location.href = "admin.html"; // This line redirects the user to the admin page

    } else {
        alert("Invalid login credentials!");
    }
}


        // Attach functions to window
        window.registerUser = registerUser;
        window.userLogin = userLogin;
        window.govtLogin = govtLogin;
    </script>
</body>
</html>