var config = { apiKey: "AIzaSyA4De5itV56yaOBfBW6Cnk3fS7skPmDCHM", authDomain: "punchlist-1561043639952.firebaseapp.com", databaseURL: "https://punchlist-1561043639952.firebaseio.com", projectId: "punchlist-1561043639952", storageBucket: "", messagingSenderId: "999467953896", appId: "1:999467953896:web:a4ded59b12ccb9ff" }; //firebase.initializeApp(firebaseConfig); if (!firebase.apps.length) { firebase.initializeApp(config); } function initApp() { // Auth state changes. // [START authstatelistener] firebase.auth().onAuthStateChanged(function(user){ console.log(`in onAuthStateChanged`); if (user) { console.log(`${user.displayName}`); // User is signed in. var displayName = user.displayName; var email = user.email; var emailVerified = user.emailVerified; var photoURL = user.photoURL; var isAnonymous = user.isAnonymous; var uid = user.uid; window.uid = uid; var providerData = user.providerData; writeUserData(uid, displayName, email, photoURL); //newPunch(uid); loadPunches(uid); document.getElementById('whoami').innerHTML = email; // [START_EXCLUDE] //document.getElementById('quickstart-sign-in-status').textContent = 'Signed in'; //document.getElementById('signout').disabled = false; //document.getElementById('quickstart-account-details').textContent = JSON.stringify(user, null, ' '); // [END_EXCLUDE] } else { // User is signed out. // [START_EXCLUDE] //document.getElementById('quickstart-sign-in-status').textContent = 'Signed out'; //document.getElementById('signout').disabled = true; //document.getElementById('quickstart-account-details').textContent = 'null'; // [END_EXCLUDE] } }); // [END authstatelistener] //document.getElementById('signout').addEventListener('click', handleSignOut, true); } // AUTH // // [START googlecallback] function onSignIn(googleUser) { console.log('Google Auth Response', googleUser); // We need to register an Observer on Firebase Auth to make sure auth is initialized. var unsubscribe = firebase.auth().onAuthStateChanged(function(firebaseUser) { unsubscribe(); // Check if we are already signed-in Firebase with the correct user. if (!isUserEqual(googleUser, firebaseUser)) { // Build Firebase credential with the Google ID token. // [START googlecredential] var credential = firebase.auth.GoogleAuthProvider.credential( googleUser.getAuthResponse().id_token); // [END googlecredential] // Sign in with credential from the Google user. // [START authwithcred] firebase.auth().signInWithCredential(credential).catch(function(error) { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // The email of the user's account used. var email = error.email; // The firebase.auth.AuthCredential type that was used. var credential = error.credential; // [START_EXCLUDE] if (errorCode === 'auth/account-exists-with-different-credential') { alert('You have already signed up with a different auth provider for that email.'); // If you are using multiple auth providers on your app you should handle linking // the user's accounts here. } else { console.error(error); } // [END_EXCLUDE] }); // [END authwithcred] } else { var user = googleUser; console.log('User already signed-in Firebase.'); var displayName = user.displayName; var email = user.email; var emailVerified = user.emailVerified; var photoURL = user.photoURL; var isAnonymous = user.isAnonymous; var uid = user.uid; var providerData = user.providerData; // [START_EXCLUDE] //document.getElementById('quickstart-sign-in-status').textContent = 'Signed in'; //document.getElementById('signout').disabled = false; //document.getElementById('quickstart-account-details').textContent = JSON.stringify(user, null, ' '); // [END_EXCLUDE] } }); } // [END googlecallback] /** * Check that the given Google user is equals to the given Firebase user. */ // [START checksameuser] function isUserEqual(googleUser, firebaseUser) { if (firebaseUser) { var providerData = firebaseUser.providerData; for (var i = 0; i < providerData.length; i++) { if (providerData[i].providerId === firebase.auth.GoogleAuthProvider.PROVIDER_ID && providerData[i].uid === googleUser.getBasicProfile().getId()) { // We don't need to reauth the Firebase connection. return true; } } } return false; } // [END checksameuser] /** * Handle the sign out button press. */ function handleSignOut() { var googleAuth = gapi.auth2.getAuthInstance(); googleAuth.signOut().then(function() { firebase.auth().signOut(); }); } function writeUserData(userId, name, email, imageUrl) { firebase.database().ref('users/' + userId).update({ username: name, email: email, profile_picture : imageUrl }); } //var punchesRef = firebase.database().ref('users/' + userId + '/punches'); //punchesRef.on('value', function(snapshot) { // updateStarCount(postElement, snapshot.val()); //}); function newPunch(uid, subject, priority, progress, needBy, notes, tags) { var punchData = { uid: uid, subject: subject, priority: priority, progress: progress, needByDate: needBy, notes: notes, tags: tags }; //Get a key for a new post var newPunchKey = firebase.database().ref().child('users/' + window.uid + '/punches').push().key; // Write the new punch data var updates = {}; updates['users/' + uid + '/punches/' + newPunchKey] = punchData; return firebase.database().ref().update(updates); } // Read the punches via listener // standard functions function clearDefault(a){ if (a.defaultValue === a.value) { a.value=""; } } // some element functions... function enableElement(element) { console.log(`enabling ${element}`); document.getElementById(element).style.display = "block"; } function disableElement(element) { console.log(`disabling ${element}`); document.getElementById(element).style.display = "none"; } // menu function mainMenuDrop() { document.getElementById("mainMenuDropdown").classList.toggle("show"); } window.onclick = function(event) { if (!event.target.matches('.dropbtn')) { var dropdowns = document.getElementsByClassName("dropdown-content"); var i; for (i = 0; i < dropdowns.length; i++) { var openDropdown = dropdowns[i]; if (openDropdown.classList.contains('show')) { openDropdown.classList.remove('show'); } } } } // load punches function genPunchListItem(elementData, element) { $( elementData ).appendTo( element ); } function loadPunches(uid) { var punchesRef = firebase.database().ref('users/' + uid + '/punches'); var itemStyle = "punches"; // list = '
Hello There!
" ).appendTo( ".punchListBacklog" ); } // old script