Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- // Initialize Firebase
- const config = {
- apiKey: "AIzaSyDBRXLeYwNvNisdtcOOhN0VM4SSoCZD80g",
- authDomain: "btc-arena.firebaseapp.com",
- databaseURL: "https://btc-arena.firebaseio.com",
- projectId: "btc-arena",
- storageBucket: "btc-arena.appspot.com",
- messagingSenderId: "453622577544"
- };
- firebase.initializeApp(config);
- var database = firebase.database();
- var db = firebase.firestore();
- var user;
- //Get Elements
- const txtEmail = document.getElementById('emailinput');
- const txtPassword = document.getElementById('passwordinput');
- const btnLogin = document.getElementById('loginbutton');
- const btnSignUp = document.getElementById('signupbutton');
- const btnLogout = document.getElementById('logoutbutton');
- const userStatus = document.getElementById('status');
- const btnOpenClient = document.getElementById('openclientbutton');
- function writeUserData(userId, name, email, imageUrl) {
- }
- //Sign in
- btnLogin.addEventListener('click', e => {
- const email = txtEmail.value;
- const pass= txtPassword.value;
- const auth = firebase.auth();
- auth.signInWithEmailAndPassword(email, pass).then(function() {
- //User successfully logged-in , do something with him.
- }).catch(function(error) {
- // An error happened.
- console.log(e.message);
- });
- });
- btnSignUp.addEventListener('click', e =>{
- const email = txtEmail.value;
- const pass= txtPassword.value;
- const auth = firebase.auth();
- const promise = auth.createUserWithEmailAndPassword(email, pass).then(function() {
- //User successfully signed-up , we logging he im immediatly to set up his account with realtime-database.
- auth.signInWithEmailAndPassword(email, pass).then(function() {
- //This is first users log-in to the platform, add some information about him to realtime-databse.
- db.collection("users").add({
- email: user.email,
- })
- .then(function(docRef) {
- console.log("Document written with ID: ", docRef.id);
- })
- .catch(function(error) {
- console.error("Error adding document: ", error);
- });
- // firebase.database().ref('users/' + user.uid).set({
- // email: user.email,
- // client: false
- // });
- }).catch(function(error) {
- // An error happened.
- console.log(e.message);
- });
- }).catch(function(error) {
- // An error happened.
- console.log(e.message);
- });
- });
- //Sign out
- btnLogout.addEventListener('click', e =>{
- firebase.auth().signOut().then(function() {
- // Sign-out successful.
- console.log("user signed out");
- }).catch(function(error) {
- // An error happened.
- console.log(e.message);
- });
- });
- //Open Client Button
- btnOpenClient.addEventListener('click', e =>{
- window.open('./btc-arena webGL/index.html','_blank');
- });
- //Realtime listener
- firebase.auth().onAuthStateChanged(firebaseUser => {
- if(firebaseUser){
- user = firebase.auth().currentUser;
- console.log("logged in as " + user.email);
- userStatus.value = "Logged in.";
- }else{
- console.log("not logged in");
- userStatus.value = "Logged Out.";
- }
- });
- //
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement