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