-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
119 lines (103 loc) · 3.53 KB
/
app.js
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
// Firebase config
const firebaseConfig = {
apiKey: "AIzaSyAi1gCQuw2Hn7EltdwLiEBfZdh4pI-B9R4",
authDomain: "simple-messaging-app-d60d5.firebaseapp.com",
databaseURL: "https://simple-messaging-app-d60d5-default-rtdb.firebaseio.com",
projectId: "simple-messaging-app-d60d5",
storageBucket: "simple-messaging-app-d60d5.firebasestorage.app",
messagingSenderId: "838083703261",
appId: "1:838083703261:web:66be6b36527ad3043c8634"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const database = firebase.database();
// DOM Elements
const loginBox = document.getElementById('loginBox');
const chatBox = document.getElementById('chatBox');
const emailInput = document.getElementById('emailInput');
const passwordInput = document.getElementById('passwordInput');
const userEmail = document.getElementById('userEmail');
const messageInput = document.getElementById('messageInput');
const messages = document.getElementById('messages');
// Buttons
const loginBtn = document.getElementById('loginBtn');
const registerBtn = document.getElementById('registerBtn');
const logoutBtn = document.getElementById('logoutBtn');
const sendBtn = document.getElementById('sendBtn');
// Handle auth state changes
function handleAuthStateChange(user) {
if (user) {
loginBox.style.display = 'none';
chatBox.style.display = 'block';
userEmail.innerText = user.email;
loadMessages();
} else {
loginBox.style.display = 'block';
chatBox.style.display = 'none';
messages.innerHTML = '';
}
}
// Login function
function handleLogin() {
const email = emailInput.value;
const password = passwordInput.value;
auth.signInWithEmailAndPassword(email, password).catch(handleError);
}
// Register function
function handleRegister() {
const email = emailInput.value;
const password = passwordInput.value;
auth.createUserWithEmailAndPassword(email, password).catch(handleError);
}
// Error handler
function handleError(error) {
alert(error.message);
}
// Load messages
function loadMessages() {
const messagesRef = database.ref('messages');
messagesRef.on('child_added', handleNewMessage);
}
// Handle new message from database
function handleNewMessage(snapshot) {
const message = snapshot.val();
console.log("New message", message)
displayMessage(message);
}
// Display a message
function displayMessage(message) {
const div = document.createElement('div');
div.className = 'message';
if (message.userId === auth.currentUser.uid) {
div.classList.add('sent');
} else {
div.classList.add('received');
const emailDiv = document.createElement('div');
emailDiv.className = 'sender-email';
emailDiv.innerText = message.userEmail;
div.appendChild(emailDiv);
}
const textDiv = document.createElement('div');
textDiv.innerText = message.text;
div.appendChild(textDiv);
messages.appendChild(div);
}
// Send message
function handleSendMessage() {
const message = messageInput.value.trim();
if (!message) return;
database.ref('messages').push({
text: message,
userId: auth.currentUser.uid,
userEmail: auth.currentUser.email,
timestamp: firebase.database.ServerValue.TIMESTAMP
});
messageInput.value = '';
}
// Add event listeners
auth.onAuthStateChanged(handleAuthStateChange);
loginBtn.onclick = handleLogin;
registerBtn.onclick = handleRegister;
logoutBtn.onclick = () => auth.signOut();
sendBtn.onclick = handleSendMessage;