Advertisement
Guest User

app.js

a guest
May 19th, 2017
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.   // Initialize Firebase
  3.   const config = {
  4.     apiKey: "AIzaSyBohS0b1b9oMqnXjcPbScGUQuVuPSTrFRg",
  5.     authDomain: "graffite-c9805.firebaseapp.com",
  6.     databaseURL: "https://graffite-c9805.firebaseio.com",
  7.     projectId: "graffite-c9805",
  8.     storageBucket: "graffite-c9805.appspot.com",
  9.     messagingSenderId: "66234725427"
  10.   };
  11.   firebase.initializeApp(config);
  12.  
  13.   // Get elements  
  14.   const txtEmail = document.getElementById('txtEmail');
  15.   const txtPassword = document.getElementById('txtPassword');
  16.   const btnLogin = document.getElementById('btnLogin');
  17.   const btnSignUp = document.getElementById('btnSignUp');
  18.   const btnLogOut = document.getElementById('btnLogout');
  19.  
  20.  
  21.  // Add login event
  22.   btnLogin.addEventListener('click', e => {
  23.      
  24.       // Get email and pass
  25.       const email = txtEmail.value;
  26.       const pass = txtPassword.value;
  27.       const auth = firebase.auth();
  28.      
  29.      
  30.       // Sign in
  31.       const promise = auth.signInWithEmailAndPassword(email, pass);
  32.       promise.catch(e => console.log(e.message));
  33.      
  34.  });
  35.    
  36.     btnSignUp.addEventListener('click', e => {
  37.       // Get email and pass
  38.       // TODO: CHECK IF E-MAIL EXISTS
  39.       const email = txtEmail.value;
  40.       const pass = txtPassword.value;
  41.       const auth = firebase.auth();
  42.      
  43.       // Sign in
  44.       const promise = auth.createUserWithEmailAndPassword(email, pass);
  45.       promise
  46.         .catch(e => console.log(e.message));
  47.       window.location.replace("app.html");  
  48.     });
  49.    
  50.     btnLogout.addEventListener('click', e => {
  51.         firebase.auth().signOut();
  52.    
  53.       // Add a realtime Listener
  54.       firebase.auth().onAuthStateChanged(firebaseUser => { if(firebaseUser) {
  55.           console.log(firebaseUser);
  56.           btnLogout.classList.remove('hide');
  57.          
  58.       } else {
  59.           console.log('not logged in');
  60.           btnLogout.classList.add('hide');
  61.       }
  62.             });
  63.         });
  64.    
  65.    
  66.    
  67. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement