Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (57 loc) · 2.14 KB
/
Copy pathscript.js
File metadata and controls
65 lines (57 loc) · 2.14 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
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.8.0/firebase-app.js";
import { getAuth, signInWithPopup, GoogleAuthProvider, signOut, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.8.0/firebase-auth.js";
const firebaseConfig = {
apiKey: "AIzaSyBxJHccT6qiZGsFBYisT8oIUw43mzFldQQ",
authDomain: "tecsubonline.firebaseapp.com",
projectId: "tecsubonline",
storageBucket: "tecsubonline.firebasestorage.app",
messagingSenderId: "105883522974",
appId: "1:105883522974:web:693279505a1eb1c29ea88f",
measurementId: "G-E2R9S024L0",
databaseURL: "https://tecsubonline-default-rtdb.firebaseio.com"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const provider = new GoogleAuthProvider();
const loginBtn = document.getElementById('loginBtn');
const logoutBtn = document.getElementById('logoutBtn');
const profileDiv = document.getElementById('profile');
const loading = document.getElementById('loading');
// Monitor Auth State
onAuthStateChanged(auth, (user) => {
if (user) {
updateUI(user);
} else {
loginBtn.style.display = 'flex';
profileDiv.style.display = 'none';
loading.style.display = 'none';
}
});
function updateUI(user) {
document.getElementById('userName').innerText = user.displayName;
document.getElementById('userEmail').innerText = user.email;
document.getElementById('userImg').src = user.photoURL;
loginBtn.style.display = 'none';
profileDiv.style.display = 'block';
loading.style.display = 'none';
}
loginBtn.onclick = () => {
loading.style.display = 'block';
loginBtn.style.display = 'none';
signInWithPopup(auth, provider)
.then((result) => {
updateUI(result.user);
})
.catch((error) => {
loading.style.display = 'none';
loginBtn.style.display = 'flex';
console.error("Login Error:", error.message);
alert("Login failed: " + error.message);
});
};
logoutBtn.onclick = () => {
signOut(auth).then(() => {
loginBtn.style.display = 'flex';
profileDiv.style.display = 'none';
});
};