Advertisement
RemonMelika

Untitled

Nov 28th, 2016
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  // Initialize Firebase
  2.   // TODO: Replace with your project's customized code snippet
  3.  
  4. (function(){
  5.  
  6.   // Initialize Firebase
  7.   var config = {
  8.     apiKey: "AIzaSyDzWxlbKReTJQyvad5DLBJ6je5e4IDpUVQ",
  9.     authDomain: "agile-30622.firebaseapp.com",
  10.     databaseURL: "https://agile-30622.firebaseio.com",
  11.     storageBucket: "agile-30622.appspot.com",
  12.     messagingSenderId: "592891433441"
  13.   };
  14.   firebase.initializeApp(config);
  15.  
  16. //get elements
  17. const txtEmail=document.getElementById('txtEmail');
  18. const txtPassword=document.getElementById('txtPassword');
  19. const btnLogin=document.getElementById('btnLogin');
  20. const btnSignup=document.getElementById('btnSignup');
  21. const btnLogout=document.getElementById('btnLogout');
  22.  
  23. //Add login event
  24. btnLogin.addEventListener('click',e=>{
  25. //Get email and pass
  26. const email=txtEmail.value;
  27. const password=txtPassword.value;
  28. const auth =firebase.auth();
  29. //sign in
  30. const promise = auth.signInWithEmailAndPassword(email,password);
  31. promise.catch(e=>console.log(e.message));
  32. });
  33.  
  34.  
  35. //Add signup event
  36. btnSignup.addEventListener('click',e=>{
  37. //Get email and pass
  38. //TODO:Check for REAL EMAIL
  39. const email=txtEmail.value;
  40. const password=txtPassword.value;
  41. const auth =firebase.auth();
  42. //sign in
  43. const promise= auth.createUserWithEmailAndPassword(email,password);
  44. promise.catch(e=>console.log(e.message));
  45. });
  46.  
  47. btnLogout.addEventListener('click',e=>{
  48. firebase.auth().signOut();
  49.  
  50. });
  51.  
  52.  
  53.  
  54. //Add a realtime listener
  55. firebase.auth().onAuthStateChanged(firebaseUser =>{
  56.     if(firebaseUser){
  57.         console.log(firebaseUser);
  58.         btnLogout.classList.remove('hide');
  59.     }
  60.     else {
  61.         console.log('not logged in');
  62.         btnLogout.classList.add('hide');
  63.     }
  64.  
  65. });
  66. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement