1(function ($) { 2 'use strict'; 3 4 if (typeof window.firebaseOptions !== 'undefined') { 5 // Initialize FirebaseApp 6 if (!firebase.apps.length) { 7 firebase.initializeApp(window.firebaseOptions); 8 } 9 10 $(document).ready(function () { 11 init(); 12 13 // Check user state 14 checkUserState().then((loggedin) => { 15 if (loggedin) { 16 action_when_login(); 17 showGreetings(); 18 } else { 19 action_when_logout(); 20 } 21 }); 22 23 // Firebase login 24 $('#firebase-form-submit').click(function (event) { 25 event.preventDefault(); 26 let data = $('#firebase-login-form :input').serializeArray(); 27 let email = data[0].value; 28 let password = data[1].value; 29 30 // start login into firebase 31 if (email !== '' && password !== '') { 32 firebase 33 .auth() 34 .signInWithEmailAndPassword(email, password) 35 .then(() => { 36 action_when_login(); 37 showGreetings(); 38 }) 39 .catch((error) => { 40 console.log(error.message); 41 $('p#firebase-login-error').show(); 42 $('p#firebase-login-error').text(error.message); 43 }); 44 } else { 45 $('p#firebase-login-error').text( 46 'Your email or password is missing!' 47 ); 48 } 49 }); 50 51 // Sign out action 52 $('#firebase-signout').on('click', (e) => { 53 e.preventDefault(); 54 firebase 55 .auth() 56 .signOut() 57 .then(() => { 58 action_when_logout(); 59 }) 60 .catch((error) => console.log(error)); 61 }); 62 63 function checkUserState() { 64 return new Promise((resolve, reject) => { 65 return firebase.auth().onAuthStateChanged(function (user) { 66 if (user) { 67 // User is signed in. 68 resolve(true); 69 } else { 70 // User is signed out. 71 resolve(false); 72 } 73 }); 74 }); 75 } 76 77 function showGreetings() { 78 firebase.auth().onAuthStateChanged(function (user) { 79 if (user) { 80 // User is signed in. 81 if (user.displayName === null) { 82 $('#firebase-user').text(`Greetings, ${user.email}!`); 83 } else { 84 $('#firebase-user').text(`Greetings, ${user.displayName}!`); 85 } 86 } else { 87 $('#firebase-user').hide(); 88 } 89 }); 90 } 91 92 // Realtime database 93 function showRealtimeDatabase() { 94 var db = firebase.database(); 95 var realtimeEl = $('#if-realtime'); 96 var collectionName = realtimeEl.data('collection-name'); 97 var documentName = realtimeEl.data('document-name'); 98 99 if (collectionName && documentName) { 100 db.ref('/' + collectionName + '/' + documentName) 101 .once('value') 102 .then(function (snapshot) { 103 var dataObj = snapshot.val(); 104 if (dataObj) { 105 var html = '<table>'; 106 $.each(dataObj, function (key, value) { 107 html += '<tr>'; 108 html += '<td>' + key + '</td>'; 109 html += '<td>' + value + '</td>'; 110 html += '</tr>'; 111 }); 112 html += '</table>'; 113 realtimeEl.append(html); 114 } else { 115 console.error( 116 'Please check your collection and document name in the [realtime] shortcode!' 117 ); 118 } 119 }); 120 } else { 121 console.warn( 122 'Please check your collection and document name in the [realtime] shortcode!' 123 ); 124 } 125 } 126 127 function init() { 128 action_when_logout(); 129 // Realtime database 130 showRealtimeDatabase(); 131 } 132 133 function action_when_login() { 134 $('.firebase-show').show(); 135 $('#firebase-signout').show(); 136 $('p#firebase-login-error').hide(); 137 $('.firebase-show-when-not-login').hide(); 138 $('#firebase-login-form').hide(); 139 } 140 141 function action_when_logout() { 142 $('.firebase-show').hide(); 143 $('#firebase-signout').hide(); 144 $('p#firebase-login-error').hide(); 145 $('.firebase-show-when-not-login').show(); 146 $('#firebase-login-form').show(); 147 } 148 });
149 } else { 150 console.warn('Please enter your Firebase settings!'); 151 } 152})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.