-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.js
More file actions
197 lines (175 loc) · 7.26 KB
/
firebase.js
File metadata and controls
197 lines (175 loc) · 7.26 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
import { initializeApp } from "https://www.gstatic.com/firebasejs/12.3.0/firebase-app.js";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged, signInWithCustomToken, signInAnonymously, signOut } from "https://www.gstatic.com/firebasejs/12.3.0/firebase-auth.js";
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyAqDLocdi1CDHDeD5Z_LOlVZOycI2tuJpk",
authDomain: "digisoria-baa83.firebaseapp.com",
projectId: "digisoria-baa83",
storageBucket: "digisoria-baa83.firebasestorage.app",
messagingSenderId: "1042609120554",
appId: "1:1042609120554:web:0500a100e9ae42ead32a53",
measurementId: "G-TM8CBPSRR9"
};
// Global variables for Firebase auth token provided by the environment
const initialAuthToken = typeof __initial_auth_token !== 'undefined' ? __initial_auth_token : null;
const messageBox = document.getElementById('messageBox');
function showMessage(message, duration = 3000) {
if (!messageBox) return;
messageBox.textContent = message;
messageBox.classList.add('show');
setTimeout(() => {
messageBox.classList.remove('show');
}, duration);
}
function togglePasswordVisibility(fieldId) {
const passwordField = document.getElementById(fieldId);
const toggleIcon = document.getElementById(`${fieldId}-toggle`);
if (passwordField.type === "password") {
passwordField.type = "text";
toggleIcon.classList.remove('fa-eye');
toggleIcon.classList.add('fa-eye-slash');
} else {
passwordField.type = "password";
toggleIcon.classList.remove('fa-eye-slash');
toggleIcon.classList.add('fa-eye');
}
}
// This makes the togglePasswordVisibility function available globally
window.togglePasswordVisibility = togglePasswordVisibility;
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
let isAuthReady = false;
// Firebase Initialization
async function initializeAuth() {
try {
if (initialAuthToken) {
await signInWithCustomToken(auth, initialAuthToken);
console.log('Signed in with custom token.');
} else {
await signInAnonymously(auth);
console.log('Signed in anonymously.');
}
} catch (error) {
console.error("Firebase auth initialization error: ", error);
showMessage(`Authentication error: ${error.message}`);
}
}
// Logout functionality
const logoutLink = document.getElementById('logout-link');
if (logoutLink) {
logoutLink.addEventListener('click', async (e) => {
e.preventDefault();
try {
await signOut(auth);
showMessage("Logout successful!");
window.location.href = 'login.html'; // Redirect to login page after logout
} catch (error) {
console.error("Logout error:", error);
showMessage(`Logout error: ${error.message}`);
}
});
}
// Listen for authentication state changes and update the UI
onAuthStateChanged(auth, (user) => {
const signinLink = document.getElementById('signin-link');
const userDisplayName = document.getElementById('user-display-name');
if (user) {
console.log("User is signed in. UID:", user.uid);
// We now wait for the user state to be confirmed before showing a message
if (isAuthReady) {
showMessage(`Logged in as: ${user.email || 'Anonymous user'}`);
}
if (signinLink && userDisplayName) {
userDisplayName.textContent = user.displayName || user.email; // Use displayName or email
signinLink.href = 'profile.html'; // Change the link to the profile page
}
} else {
console.log("User is signed out.");
if (signinLink && userDisplayName) {
userDisplayName.textContent = 'Sign in';
signinLink.href = 'login.html';
}
}
isAuthReady = true;
});
initializeAuth();
// Form Submissions
const loginForm = document.getElementById('login');
if (loginForm) {
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('loginEmail').value;
const password = document.getElementById('loginPassword').value;
try {
await signInWithEmailAndPassword(auth, email, password);
showMessage("Login successful!");
window.location.href = 'seed.html'; // Add this line to redirect
} catch (error) {
let errorMessage = "An error occurred during login. Please try again.";
if (error.code === 'auth/wrong-password' || error.code === 'auth/user-not-found') {
errorMessage = "Invalid email or password.";
} else {
console.error("Login error:", error);
}
showMessage(errorMessage);
}
});
}
const signupForm = document.getElementById('signup');
if (signupForm) {
signupForm.addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('signupEmail').value;
const password = document.getElementById('signupPassword').value;
const confirmPassword = document.getElementById('signupConfirmPassword').value;
if (password !== confirmPassword) {
showMessage("Passwords do not match.");
return;
}
if (password.length < 6) {
showMessage("Password must be at least 6 characters long.");
return;
}
try {
await createUserWithEmailAndPassword(auth, email, password);
showMessage("Account created successfully!");
window.location.href = 'login.html';
} catch (error) {
let errorMessage = "An error occurred during signup. Please try again.";
if (error.code === 'auth/email-already-in-use') {
errorMessage = "This email is already in use.";
} else if (error.code === 'auth/invalid-email') {
errorMessage = "The email address is not valid.";
} else if (error.code === 'auth/weak-password') {
errorMessage = "Password is too weak. Please choose a stronger password.";
} else {
console.error("Signup error:", error);
}
showMessage(errorMessage);
}
});
}
// UI Toggle
const loginContainer = document.getElementById('loginForm');
const signupContainer = document.getElementById('signupForm');
const showSignupBtn = document.getElementById('showSignup');
const showLoginBtn = document.getElementById('showLogin');
if (showSignupBtn && loginContainer && signupContainer) {
showSignupBtn.addEventListener('click', (e) => {
e.preventDefault();
loginContainer.classList.remove('visible');
loginContainer.classList.add('hidden');
signupContainer.classList.remove('hidden');
signupContainer.classList.add('visible');
});
}
if (showLoginBtn && signupContainer && loginContainer) {
showLoginBtn.addEventListener('click', (e) => {
e.preventDefault();
signupContainer.classList.remove('visible');
signupContainer.classList.add('hidden');
loginContainer.classList.remove('hidden');
loginContainer.classList.add('visible');
});
}