|
| 1 | +// Initialize Firebase |
| 2 | +const firebaseConfig = { |
| 3 | + apiKey: "AIzaSyBYM0tkjdUDSzz7UoJt92jQITLXk4DD8lE", |
| 4 | + authDomain: "stdperformance-bf994.firebaseapp.com", |
| 5 | + projectId: "stdperformance-bf994", |
| 6 | + storageBucket: "stdperformance-bf994.appspot.com", |
| 7 | + messagingSenderId: "990002419810", |
| 8 | + appId: "1:990002419810:web:e61ff22f2e52e7664fd93b", |
| 9 | + measurementId: "G-346DHF7S46" |
| 10 | +}; |
| 11 | + |
| 12 | +// Ensure Firebase isn't initialized more than once |
| 13 | +if (!firebase.apps.length) { |
| 14 | + firebase.initializeApp(firebaseConfig); |
| 15 | +} else { |
| 16 | + firebase.app(); // if already initialized, use that one |
| 17 | +} |
| 18 | + |
| 19 | +const auth = firebase.auth(); |
| 20 | + |
| 21 | +const signInForm = document.querySelector('.sign-in-form'); |
| 22 | +const signUpForm = document.querySelector('.sign-up-form'); |
| 23 | +const container = document.querySelector('.container'); |
| 24 | + |
| 25 | +document.querySelector('#sign-up-btn').addEventListener('click', () => { |
| 26 | + container.classList.add('sign-up-mode'); |
| 27 | +}); |
| 28 | + |
| 29 | +document.querySelector('#sign-in-btn').addEventListener('click', () => { |
| 30 | + container.classList.remove('sign-up-mode'); |
| 31 | +}); |
| 32 | + |
| 33 | +signUpForm.addEventListener('submit', async (e) => { |
| 34 | + e.preventDefault(); |
| 35 | + const email = signUpForm.querySelector('input[type=email]').value; |
| 36 | + const password = signUpForm.querySelector('input[type=password]').value; |
| 37 | + |
| 38 | + try { |
| 39 | + const userCredential = await auth.createUserWithEmailAndPassword(email, password); |
| 40 | + console.log('User signed up:', userCredential.user); |
| 41 | + alert('You have successfully signed up!'); |
| 42 | + signUpForm.reset(); |
| 43 | + container.classList.remove('sign-up-mode'); |
| 44 | + |
| 45 | + } catch (error) { |
| 46 | + console.error('Error creating user:', error); |
| 47 | + alert('Error creating user: ' + error.message); |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +signInForm.addEventListener('submit', async (e) => { |
| 52 | + e.preventDefault(); |
| 53 | + const email = signInForm.querySelector('input[type=email]').value; |
| 54 | + const password = signInForm.querySelector('input[type=password]').value; |
| 55 | + |
| 56 | + try { |
| 57 | + const userCredential = await auth.signInWithEmailAndPassword(email, password); |
| 58 | + console.log('User signed in:', userCredential.user); |
| 59 | + alert('You have successfully signed in!'); |
| 60 | + signInForm.reset(); |
| 61 | + window.location.href = 'profile.html'; |
| 62 | + } catch (error) { |
| 63 | + console.error('Error user Not found:', error); |
| 64 | + alert('Error User Not Found: ' + error.message); |
| 65 | + } |
| 66 | +}); |
0 commit comments