-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
230 lines (177 loc) · 4.97 KB
/
Copy pathapp.js
File metadata and controls
230 lines (177 loc) · 4.97 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
218
219
220
221
222
223
224
225
226
227
228
229
230
import { initializeApp } from "https://www.gstatic.com/firebasejs/12.10.0/firebase-app.js";
import {
getAuth,
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
signOut,
onAuthStateChanged
} from "https://www.gstatic.com/firebasejs/12.10.0/firebase-auth.js";
import {
getFirestore,
doc,
setDoc,
getDoc
} from "https://www.gstatic.com/firebasejs/12.10.0/firebase-firestore.js";
// 🔹 FIREBASE CONFIG
const firebaseConfig = {
apiKey: "AIzaSyAQU7n-P6lirLhXhyLuOm9JL-dnIf-j-2U",
authDomain: "resqlink-73b57.firebaseapp.com",
projectId: "resqlink-73b57",
storageBucket: "resqlink-73b57.firebasestorage.app",
messagingSenderId: "1089979527311",
appId: "1:1089979527311:web:978de63a5d76348d7440fa"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
// 🔹 SCREEN CONTROL
const screens = document.querySelectorAll(".screen");
function show(screenId) {
screens.forEach(s => s.classList.remove("active"));
document.getElementById(screenId).classList.add("active");
}
// loading → login
setTimeout(() => show("login"), 2000);
// 🔹 NAVIGATION
goRegister.onclick = () => show("register");
goLogin.onclick = () => show("login");
// 🔹 REGISTER
registerBtn.onclick = async () => {
try {
const userCred = await createUserWithEmailAndPassword(
auth,
regEmail.value.trim(),
regPassword.value.trim()
);
await setDoc(doc(db, "users", userCred.user.uid), {
name: regName.value.trim(),
guardian: regGuardian.value.trim()
});
alert("✔ Registration Completed Successfully");
show("login");
} catch (error) {
alert("Registration Error: " + error.code);
}
};
// 🔹 LOGIN
loginBtn.onclick = async () => {
loginError.innerText = "";
try {
await signInWithEmailAndPassword(
auth,
loginEmail.value.trim(),
loginPassword.value.trim()
);
} catch (error) {
console.log(error);
if (error.code === "auth/wrong-password") {
loginError.innerText = "Invalid password";
}
else if (error.code === "auth/user-not-found") {
loginError.innerText = "User not found";
}
else if (error.code === "auth/invalid-credential") {
loginError.innerText = "Invalid email or password";
}
else {
loginError.innerText = error.code;
}
}
};
// 🔹 AUTH STATE LISTENER (VERY IMPORTANT FIX)
onAuthStateChanged(auth, async (user) => {
if (!user) return;
try {
const userDoc = await getDoc(doc(db, "users", user.uid));
if (!userDoc.exists()) {
alert("User data missing in Firestore.");
return;
}
const userData = userDoc.data();
greeting.innerText = "Welcome, " + userData.name;
show("dashboard");
initLocation();
} catch (error) {
alert("Firestore Read Error: " + error.message);
}
});
// 🔹 LOGOUT
logoutBtn.onclick = async () => {
await signOut(auth);
show("login");
};
// 🔹 LOCATION
function initLocation() {
if (!navigator.geolocation) {
locationStatus.innerText = "Geolocation not supported";
return;
}
navigator.geolocation.getCurrentPosition(
(pos) => {
window.lat = pos.coords.latitude;
window.lng = pos.coords.longitude;
locationStatus.innerText = "Location Ready";
mapFrame.src =
`https://maps.google.com/maps?q=${window.lat},${window.lng}&z=15&output=embed`;
},
(err) => {
locationStatus.innerText = "Location Permission Denied";
},
{ enableHighAccuracy: true }
);
}
// 🔹 SOS BUTTON (ANDROID SAFE FORMAT)
sosBtn.onclick = async () => {
const user = auth.currentUser;
if (!user) {
alert("User not authenticated");
return;
}
if (!window.lat || !window.lng) {
alert("Location not ready yet");
return;
}
try {
const snap = await getDoc(doc(db, "users", user.uid));
if (!snap.exists()) {
alert("Guardian number not found");
return;
}
const guardian = snap.data().guardian.trim();
const message =
`🚨 EMERGENCY ALERT\n` +
`Live Location:\n` +
`https://www.google.com/maps?q=${window.lat},${window.lng}`;
// Android compatible SMS format
const smsURL =
`sms:${guardian}?body=${encodeURIComponent(message)}`;
window.location.href = smsURL;
addActivity("SOS Sent");
} catch (error) {
alert("SOS Error: " + error.message);
}
};
// 🔹 ACTIVITY LOG
function addActivity(text) {
const li = document.createElement("li");
li.innerText =
new Date().toLocaleTimeString() + " - " + text;
activityList.prepend(li);
}
// 🔹 BLUETOOTH CONNECT
bluetoothBtn.onclick = async () => {
if (!navigator.bluetooth) {
alert("Web Bluetooth not supported in this browser.");
return;
}
try {
await navigator.bluetooth.requestDevice({
acceptAllDevices: true
});
connectionDot.classList.add("connected");
connectionText.innerText = "Stick Connected";
addActivity("Bluetooth Connected");
} catch (error) {
console.log(error);
}
};