|
8 | 8 | <script src="https://www.gstatic.com/firebasejs/10.7.1/firebase-analytics.js"></script>
|
9 | 9 | <script src="https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js"></script>
|
10 | 10 | <script>
|
11 |
| - // Your web app's Firebase configuration |
12 |
| - const firebaseConfig = { |
13 |
| - apiKey: "AIzaSyBH3LvyIKTVeEZ4KYFbxFAWumn2B9GtzTE", |
14 |
| - authDomain: "cmepass-391c3.firebaseapp.com", |
15 |
| - projectId: "cmepass-391c3", |
16 |
| - storageBucket: "cmepass-391c3.appspot.com", |
17 |
| - messagingSenderId: "495360510420", |
18 |
| - appId: "1:495360510420:web:338107bc68b5f0bb7e2e41", |
19 |
| - measurementId: "G-DL15HN05NG" |
20 |
| - }; |
| 11 | + document.addEventListener('DOMContentLoaded', function() { |
| 12 | + // Your web app's Firebase configuration |
| 13 | + const firebaseConfig = { |
| 14 | + apiKey: "AIzaSyBH3LvyIKTVeEZ4KYFbxFAWumn2B9GtzTE", |
| 15 | + authDomain: "cmepass-391c3.firebaseapp.com", |
| 16 | + projectId: "cmepass-391c3", |
| 17 | + storageBucket: "cmepass-391c3.appspot.com", |
| 18 | + messagingSenderId: "495360510420", |
| 19 | + appId: "1:495360510420:web:338107bc68b5f0bb7e2e41", |
| 20 | + measurementId: "G-DL15HN05NG" |
| 21 | + }; |
21 | 22 |
|
22 |
| - // Initialize Firebase |
23 |
| - firebase.initializeApp(firebaseConfig); |
24 |
| - firebase.analytics(); |
25 |
| - const auth = firebase.auth(); |
| 23 | + // Initialize Firebase |
| 24 | + firebase.initializeApp(firebaseConfig); |
| 25 | + firebase.analytics(); |
| 26 | + const auth = firebase.auth(); |
26 | 27 |
|
27 |
| - // Sign in function |
28 |
| - function signIn() { |
29 |
| - var email = document.getElementById('email').value; |
30 |
| - var password = document.getElementById('password').value; |
31 |
| - auth.signInWithEmailAndPassword(email, password) |
32 |
| - .then((userCredential) => { |
33 |
| - // Signed in |
34 |
| - document.getElementById('status').innerText = 'Logged in successfully. Welcome, ' + userCredential.user.email; |
35 |
| - }) |
36 |
| - .catch((error) => { |
37 |
| - var errorCode = error.code; |
38 |
| - var errorMessage = error.message; |
39 |
| - document.getElementById('status').innerText = 'Error: ' + errorMessage; |
40 |
| - }); |
41 |
| - } |
| 28 | + // Handle form submission |
| 29 | + document.getElementById('login-form').addEventListener('submit', function(e) { |
| 30 | + e.preventDefault(); |
| 31 | + var email = document.getElementById('email').value; |
| 32 | + var password = document.getElementById('password').value; |
| 33 | + auth.signInWithEmailAndPassword(email, password) |
| 34 | + .then((userCredential) => { |
| 35 | + document.getElementById('status').innerText = 'Logged in successfully. Welcome, ' + userCredential.user.email; |
| 36 | + }) |
| 37 | + .catch((error) => { |
| 38 | + var errorCode = error.code; |
| 39 | + var errorMessage = error.message; |
| 40 | + document.getElementById('status').innerText = 'Error: ' + errorMessage; |
| 41 | + }); |
| 42 | + }); |
42 | 43 |
|
43 |
| - // Auth state changes |
44 |
| - auth.onAuthStateChanged((user) => { |
45 |
| - if (user) { |
46 |
| - // User is signed in |
47 |
| - document.getElementById('status').innerText = 'User is currently signed in'; |
48 |
| - } else { |
49 |
| - // User is signed out |
50 |
| - document.getElementById('status').innerText = 'User is signed out'; |
51 |
| - } |
| 44 | + // Auth state changes |
| 45 | + auth.onAuthStateChanged((user) => { |
| 46 | + if (user) { |
| 47 | + document.getElementById('status').innerText = 'User is currently signed in'; |
| 48 | + } else { |
| 49 | + document.getElementById('status').innerText = 'User is signed out'; |
| 50 | + } |
| 51 | + }); |
52 | 52 | });
|
53 | 53 | </script>
|
54 | 54 | <style>
|
|
81 | 81 | margin-top: 20px;
|
82 | 82 | color: #d9534f;
|
83 | 83 | }
|
| 84 | + form { |
| 85 | + display: inline-block; |
| 86 | + text-align: left; |
| 87 | + } |
84 | 88 | </style>
|
85 | 89 | </head>
|
86 | 90 | <body>
|
87 | 91 | <h1>Firebase Authentication Example</h1>
|
88 |
| - <input type="email" id="email" placeholder="Email"> |
89 |
| - <input type="password" id="password" placeholder="Password"> |
90 |
| - <button onclick="signIn()">Sign In</button> |
| 92 | + <form id="login-form"> |
| 93 | + <input type="email" id="email" placeholder="Email"> |
| 94 | + <input type="password" id="password" placeholder="Password"> |
| 95 | + <button type="submit">Sign In</button> |
| 96 | + </form> |
91 | 97 | <div id="status"></div>
|
92 | 98 | </body>
|
93 | 99 | </html>
|
0 commit comments