Created
May 19, 2016 08:09
-
-
Save deanhume/60c1fdfc97c3ce2ebd4cfd4ac633b8c1 to your computer and use it in GitHub Desktop.
Firebase authentication store data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var messagesRef = new Firebase('https://brilliant-fire-3159.firebaseio.com/users'); | |
var userId = 0; | |
// For user authentication | |
function authHandler(error, authData) { | |
if (error) { | |
console.log('Login Failed!', error); | |
} else { | |
// Set the gravatar | |
document.getElementById('gravatar').src = authData.password.profileImageURL; | |
} | |
} | |
// Log the user in with an email combination | |
messagesRef.authWithPassword({ | |
email : '[email protected]', | |
password : 'dean123' | |
}, authHandler); | |
messagesRef.onAuth(function(authData) { | |
userId = authData.uid; | |
}); | |
var messageField = document.getElementById('messageInput'); | |
var messageResults = document.getElementById('results'); | |
// Save data to firebase | |
function savedata(){ | |
var message = messageField.value; | |
messagesRef.child('users').child(userId).push( | |
{ | |
fieldName:'messageField', | |
text:message | |
}); | |
messageField.value = ''; | |
} | |
// Update results when data is added | |
messagesRef.child('users').child(userId).limitToLast(10).on('child_added', function (snapshot) { | |
var data = snapshot.val(); | |
var message = data.text; | |
if (message != undefined) | |
{ | |
messageResults.value += '\n' + message; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment