Advertisement
Guest User

app.js

a guest
May 19th, 2017
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.   // Inicializa o 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.   // Pega os elementos HTML
  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.   const btnFacebookLogin = document.getElementById('btnFacebookLogin');
  20.  
  21.   var provider = new firebase.auth.FacebookAuthProvider();
  22.  
  23.   // Adiciona o Evento de Login do Facebook
  24.   btnFacebookLogin.addEventListener('click', e => {
  25.       firebase.auth().signInWithRedirect(provider);
  26.       firebase.auth().getRedirectResult().then(function(result) {
  27.           if(result.credential) {
  28.               // Gera o Token pra acessar a API do Facebook
  29.               var token = result.credential.accessToken;
  30.           }
  31.           var user = result.user;
  32.           // Informação do Usuário
  33.       }).catch(function(error){
  34.           // Lida com os erros
  35.           var errorCode = error.code;
  36.           var errorMessage = error.message;
  37.           // O e-mail da conta de usuário utilizada
  38.           var email = error.email;
  39.           // O type firebase.auth.AuthCredential que foi usado
  40.           var credential = error.credential;
  41.       });
  42.       window.location.replace("app.html"); 
  43.   });
  44.  
  45.  // Adiciona Evento de Login
  46.   btnLogin.addEventListener('click', e => {
  47.      
  48.       // Pega E-mail e Senha
  49.       const email = txtEmail.value;
  50.       const pass = txtPassword.value;
  51.       const auth = firebase.auth();
  52.      
  53.      
  54.       // Loga
  55.       const promise = auth.signInWithEmailAndPassword(email, pass);
  56.       promise.catch(e => console.log(e.message));
  57.      
  58.  });
  59.    
  60.     btnSignUp.addEventListener('click', e => {
  61.       // Pega Email e Senha
  62.       // A FAZER: CHECA SE O E-MAIL EXISTE
  63.       const email = txtEmail.value;
  64.       const pass = txtPassword.value;
  65.       const auth = firebase.auth();
  66.      
  67.       // Loga
  68.       const promise = auth.createUserWithEmailAndPassword(email, pass);
  69.       promise
  70.         .catch(e => console.log(e.message));
  71.       window.location.replace("app.html");  
  72.     });
  73.    
  74.     btnLogout.addEventListener('click', e => {
  75.         firebase.auth().signOut();
  76.    
  77.       // Adiciona um Listener em Tempo Real
  78.       firebase.auth().onAuthStateChanged(firebaseUser => { if(firebaseUser) {
  79.           console.log(firebaseUser);
  80.           btnLogout.classList.remove('hide');
  81.          
  82.       } else {
  83.           console.log('not logged in');
  84.           btnLogout.classList.add('hide');
  85.       }
  86.             });
  87.         });
  88.    
  89.    
  90.    
  91. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement