Firebase requires a few header tags and to be initialized
<script src="https://www.gstatic.com/firebasejs/5.0.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.0.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.0.0/firebase-database.js"></script>
<script>
var firebaseConfig = {
apiKey: "<KEY GOES HERE>",
authDomain: "login-database-brc.firebaseapp.com",
databaseURL: "https://login-database-brc.firebaseio.com",
projectId: "login-database-brc",
storageBucket: "login-database-brc.appspot.com",
messagingSenderId: "<SENDERID>",
appId: "<APPID>",
measurementId: "<MEASUREMENTID>"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
All work is done though element actions
I first initialize variables (not sure they’re all needed
function(instance, context) {
var data
instance.data.cow =''
instance.data.arrayOfData=[]
instance.data.formats = []
instance.data.kidsData = []
instance.data.loggedIn = 'no'
instance.publishState('logged_in', instance.data.loggedin)
}
I then tell the plugin to update states when input fields change.
function(instance, properties, context) {
//set States
instance.publishState('child_name', properties.childname)
instance.publishState('behavior_to_review', properties.behavior)
//set plugin variables
instance.data.name = properties.childname;
instance.data.behavior = properties.behavior;
instance.data.format = properties.format_of_recording
instance.data.email = properties.email
instance.data.password = properties.password
}
I created the action “sign in and get data”
function(instance, properties, context) {
var starting_List = [];
var return_List = [];
var email = instance.data.email;
var password = instance.data.password;
var loggedIn = ''
//sign in
firebase.auth().signInWithEmailAndPassword(email, password)
.then((user) => {
console.log('signedIn')
loggedIn = 'yes'
instance.publishState('logged_in', loggedIn)
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage)
});
//when the user is signed in, get the userID
firebase.auth().onAuthStateChanged((user) => {
if (user) {
var userId = user.uid;
firebase.database().ref('/BRC/' + userId + '/data').once('value').then((snapshot) => {
snapshot.val().forEach(element => {
instance.data.arrayOfData.push(element)
if (element.childname != null ){
starting_List.push(element.childname);
}
})
//return the data lists
let return_List = starting_List.filter((c, index) => {return starting_List.indexOf(c) === index;});
instance.publishState("child_list", return_List);
});
} else {/* what to do if user is signed out*/}
});
}
I would love some help clearing this stuff up. is anyone else familiar with the firebase SDK and interested in helping me learn?