Advertisement
Saldiklis

Untitled

Jan 30th, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.  
  3.    
  4.     // Initialize Firebase
  5.     const config = {
  6.         apiKey: "AIzaSyDBRXLeYwNvNisdtcOOhN0VM4SSoCZD80g",
  7.         authDomain: "btc-arena.firebaseapp.com",
  8.         databaseURL: "https://btc-arena.firebaseio.com",
  9.         projectId: "btc-arena",
  10.         storageBucket: "btc-arena.appspot.com",
  11.         messagingSenderId: "453622577544"
  12.       };
  13.  
  14.       firebase.initializeApp(config);
  15.       var database = firebase.database();
  16.       var db = firebase.firestore();
  17.       var user;
  18.  
  19.  
  20.       //Get Elements
  21.       const txtEmail = document.getElementById('emailinput');
  22.       const txtPassword = document.getElementById('passwordinput');
  23.       const btnLogin = document.getElementById('loginbutton');
  24.       const btnSignUp = document.getElementById('signupbutton');
  25.       const btnLogout = document.getElementById('logoutbutton');
  26.       const userStatus = document.getElementById('status');
  27.       const btnOpenClient = document.getElementById('openclientbutton');
  28.      
  29.      
  30.       function writeUserData(userId, name, email, imageUrl) {
  31.        
  32.       }
  33.  
  34.     //Sign in
  35.      btnLogin.addEventListener('click', e => {
  36.         const email = txtEmail.value;
  37.         const pass=  txtPassword.value;
  38.         const auth = firebase.auth();
  39.         auth.signInWithEmailAndPassword(email, pass).then(function() {
  40.            
  41.           //User successfully logged-in , do something with him.
  42.  
  43.         }).catch(function(error) {
  44.           // An error happened.
  45.           console.log(e.message);
  46.         });
  47.        
  48.       });
  49.  
  50.         btnSignUp.addEventListener('click', e =>{
  51.         const email = txtEmail.value;
  52.         const pass=  txtPassword.value;
  53.         const auth = firebase.auth();
  54.         const promise = auth.createUserWithEmailAndPassword(email, pass).then(function() {                
  55.                     //User successfully signed-up , we logging he im immediatly to set up his account with realtime-database.
  56.                     auth.signInWithEmailAndPassword(email, pass).then(function() {                    
  57.                                 //This is first users log-in to the platform, add some information about him to realtime-databse.
  58.                                 db.collection("users").add({
  59.                                   email: user.email,
  60.                                  
  61.                               })
  62.                               .then(function(docRef) {
  63.                                   console.log("Document written with ID: ", docRef.id);
  64.                               })
  65.                               .catch(function(error) {
  66.                                   console.error("Error adding document: ", error);
  67.                               });
  68.                              
  69.                                // firebase.database().ref('users/' + user.uid).set({
  70.                                //   email: user.email,
  71.                                //   client: false                                
  72.                               //  });
  73.                               }).catch(function(error) {
  74.                                 // An error happened.
  75.                                 console.log(e.message);
  76.                               });
  77.  
  78.                   }).catch(function(error) {
  79.                     // An error happened.
  80.                     console.log(e.message);
  81.                   });
  82.       });
  83.  
  84.      
  85.            //Sign out
  86.            btnLogout.addEventListener('click', e =>{
  87.             firebase.auth().signOut().then(function() {
  88.                 // Sign-out successful.
  89.                 console.log("user signed out");
  90.               }).catch(function(error) {
  91.                 // An error happened.
  92.                 console.log(e.message);
  93.               });
  94.           });
  95.  
  96.       //Open Client Button
  97.       btnOpenClient.addEventListener('click', e =>{
  98.         window.open('./btc-arena webGL/index.html','_blank');
  99.       });
  100.  
  101.      
  102.      
  103.  
  104.  
  105.  
  106.       //Realtime listener
  107.       firebase.auth().onAuthStateChanged(firebaseUser => {
  108.         if(firebaseUser){
  109.             user = firebase.auth().currentUser;
  110.             console.log("logged in as " + user.email);
  111.             userStatus.value = "Logged in.";
  112.         }else{
  113.             console.log("not logged in");
  114.             userStatus.value = "Logged Out.";
  115.         }
  116.  
  117.       });    
  118. //
  119.  
  120.  
  121.      
  122.  
  123. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement