Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- לשים בתוך HTML בBODY
- <script src="https://www.gstatic.com/firebasejs/11.1.0/firebase-app-compat.js"></script>
- <script src="https://www.gstatic.com/firebasejs/11.1.0/firebase-auth-compat.js"></script>
- <script src="https://www.gstatic.com/firebasejs/11.1.0/firebase-database-compat.js"></script>
- <script src="script.js"></script>
- לשים בתוך JS ולשנות כצורך
- // Firebase configuration
- const firebaseConfig = {
- apiKey: "AIzaSyAjG_MOsClpLU7N7o-D1PkI08_tFO84w-c",
- authDomain: "facerecognition-tank.firebaseapp.com",
- databaseURL: "https://facerecognition-tank-default-rtdb.firebaseio.com",
- projectId: "facerecognition-tank",
- storageBucket: "facerecognition-tank.appspot.com",
- messagingSenderId: "253443593668",
- appId: "1:253443593668:web:735fa5d04d8138cb3d4091",
- measurementId: "G-SQ0XBX3MN2"
- };
- firebase.initializeApp(firebaseConfig);
- const auth = firebase.auth();
- // DOM elements
- const loggedOutContent = document.getElementById('logged-out-content');
- const loggedInContent = document.getElementById('logged-in-content');
- const loginForm = document.getElementById('login-form');
- const registerForm = document.getElementById('register-form');
- const logoutButton = document.getElementById('logout-btn');
- // Check if a user is logged in on page load
- auth.onAuthStateChanged((user) => {
- if (user) {
- // User is logged in
- sessionStorage.setItem('loggedIn', true); // Optional: Store state locally
- loggedOutContent.style.display = 'none';
- loggedInContent.style.display = 'block';
- } else {
- // User is logged out
- sessionStorage.removeItem('loggedIn'); // Clear state
- loggedOutContent.style.display = 'block';
- loggedInContent.style.display = 'none';
- }
- });
- // Handle login
- loginForm.addEventListener('submit', (e) => {
- e.preventDefault();
- const email = document.getElementById('login-email').value;
- const password = document.getElementById('login-password').value;
- auth.signInWithEmailAndPassword(email, password)
- .then(() => {
- alert('Login successful!');
- })
- .catch((error) => {
- alert('Login error: ' + error.message);
- });
- });
- // Handle registration
- registerForm.addEventListener('submit', (e) => {
- e.preventDefault();
- const email = document.getElementById('register-email').value;
- const password = document.getElementById('register-password').value;
- auth.createUserWithEmailAndPassword(email, password)
- .then(() => {
- alert('Registration successful!');
- })
- .catch((error) => {
- alert('Registration error: ' + error.message);
- });
- });
- // Handle logout
- logoutButton.addEventListener('click', () => {
- auth.signOut()
- .then(() => {
- alert('You have logged out.');
- window.location.href = "index.html";
- })
- .catch((error) => {
- alert('Logout error: ' + error.message);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement