Advertisement
solielios

חיבור הפיירבייס לאתר

Dec 26th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. לשים בתוך HTML בBODY
  2.  
  3.  
  4. <script src="https://www.gstatic.com/firebasejs/11.1.0/firebase-app-compat.js"></script>
  5. <script src="https://www.gstatic.com/firebasejs/11.1.0/firebase-auth-compat.js"></script>
  6. <script src="https://www.gstatic.com/firebasejs/11.1.0/firebase-database-compat.js"></script>
  7. <script src="script.js"></script>
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. לשים בתוך JS ולשנות כצורך
  16.  
  17.  
  18. // Firebase configuration
  19. const firebaseConfig = {
  20. apiKey: "AIzaSyAjG_MOsClpLU7N7o-D1PkI08_tFO84w-c",
  21. authDomain: "facerecognition-tank.firebaseapp.com",
  22. databaseURL: "https://facerecognition-tank-default-rtdb.firebaseio.com",
  23. projectId: "facerecognition-tank",
  24. storageBucket: "facerecognition-tank.appspot.com",
  25. messagingSenderId: "253443593668",
  26. appId: "1:253443593668:web:735fa5d04d8138cb3d4091",
  27. measurementId: "G-SQ0XBX3MN2"
  28. };
  29.  
  30. firebase.initializeApp(firebaseConfig);
  31. const auth = firebase.auth();
  32.  
  33. // DOM elements
  34. const loggedOutContent = document.getElementById('logged-out-content');
  35. const loggedInContent = document.getElementById('logged-in-content');
  36. const loginForm = document.getElementById('login-form');
  37. const registerForm = document.getElementById('register-form');
  38. const logoutButton = document.getElementById('logout-btn');
  39.  
  40. // Check if a user is logged in on page load
  41. auth.onAuthStateChanged((user) => {
  42. if (user) {
  43. // User is logged in
  44. sessionStorage.setItem('loggedIn', true); // Optional: Store state locally
  45. loggedOutContent.style.display = 'none';
  46. loggedInContent.style.display = 'block';
  47. } else {
  48. // User is logged out
  49. sessionStorage.removeItem('loggedIn'); // Clear state
  50. loggedOutContent.style.display = 'block';
  51. loggedInContent.style.display = 'none';
  52. }
  53. });
  54.  
  55. // Handle login
  56. loginForm.addEventListener('submit', (e) => {
  57. e.preventDefault();
  58. const email = document.getElementById('login-email').value;
  59. const password = document.getElementById('login-password').value;
  60.  
  61. auth.signInWithEmailAndPassword(email, password)
  62. .then(() => {
  63. alert('Login successful!');
  64. })
  65. .catch((error) => {
  66. alert('Login error: ' + error.message);
  67. });
  68. });
  69.  
  70. // Handle registration
  71. registerForm.addEventListener('submit', (e) => {
  72. e.preventDefault();
  73. const email = document.getElementById('register-email').value;
  74. const password = document.getElementById('register-password').value;
  75.  
  76. auth.createUserWithEmailAndPassword(email, password)
  77. .then(() => {
  78. alert('Registration successful!');
  79. })
  80. .catch((error) => {
  81. alert('Registration error: ' + error.message);
  82. });
  83. });
  84.  
  85. // Handle logout
  86. logoutButton.addEventListener('click', () => {
  87. auth.signOut()
  88. .then(() => {
  89. alert('You have logged out.');
  90. window.location.href = "index.html";
  91. })
  92. .catch((error) => {
  93. alert('Logout error: ' + error.message);
  94. });
  95. });
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement