141 lines
4.4 KiB
JavaScript
141 lines
4.4 KiB
JavaScript
// Project Submit
|
|
function submitProject() {
|
|
var data = {
|
|
title: document.getElementById("new-project-title").value,
|
|
link: document.getElementById("new-project-link").value,
|
|
status: document.getElementById("new-project-status").value,
|
|
description: document.getElementById("new-project-description").value
|
|
}
|
|
firebase.database().ref("projects/"+document.getElementById("new-project-title").value).set(data)
|
|
}
|
|
|
|
// Pull Project List and Display in Option
|
|
function projectList() {
|
|
database = firebase.database().ref("projects");
|
|
database.once('value', function(snapshot) {
|
|
snapshot.forEach(function(child) {
|
|
var a = document.createElement("option")
|
|
a.setAttribute("value",child["key"])
|
|
a.innerHTML = child["key"]
|
|
document.getElementById("project-list").appendChild(a)
|
|
})
|
|
});
|
|
}
|
|
|
|
|
|
// Make completly sure user is authenticated.
|
|
function verify() {
|
|
firebase.auth().onAuthStateChanged(function(user) {
|
|
if (user) {
|
|
readData("users", function(array) {
|
|
if (array[firebase.auth().currentUser.uid]) {
|
|
// Do nothing
|
|
} else {
|
|
window.location.href = "unauthorized.html"
|
|
}});
|
|
} else {
|
|
window.location.href = "unauthorized.html"
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
// On pages load
|
|
function onLoad() {
|
|
document.getElementById("logoutbutton").style.display = "none"
|
|
startFirebase();
|
|
checkLogin();
|
|
}
|
|
// Starts Firebase
|
|
function startFirebase() {
|
|
// Initialize Firebase
|
|
var config = {
|
|
apiKey: "AIzaSyBG5-wv3bpeiUjR6BH5GXamqPlH1arXt8Y",
|
|
authDomain: "homepage-projects-3d870.firebaseapp.com",
|
|
databaseURL: "https://homepage-projects-3d870.firebaseio.com",
|
|
projectId: "homepage-projects-3d870",
|
|
storageBucket: "homepage-projects-3d870.appspot.com",
|
|
messagingSenderId: "487531629040"
|
|
};
|
|
firebase.initializeApp(config);
|
|
}
|
|
|
|
// Firebase Check Log In Status
|
|
function checkLogin() {
|
|
firebase.auth().onAuthStateChanged(function(user) {
|
|
if (user) {
|
|
readData("users", function(array) {
|
|
if (array[firebase.auth().currentUser.uid]) {
|
|
document.getElementById("loginbutton").innerHTML = "<a href='dashboard.html'>Dashboard</a>"
|
|
var logoutnode = document.createElement("li");
|
|
document.getElementById("logoutbutton").style.display = "block"
|
|
} else {
|
|
document.getElementById("loginbutton").innerHTML = "Welcome, "+firebase.auth().currentUser.displayName
|
|
document.getElementById("logoutbutton").style.display = "block"
|
|
}
|
|
})} else {
|
|
document.getElementById("loginbutton").innerHTML = "<a href='javascript:logIn();'>Log In</a>"
|
|
document.getElementById("logoutbutton").style.display = "none"
|
|
}
|
|
})
|
|
}
|
|
|
|
// Firebase Log In
|
|
function logIn() {
|
|
firebase.auth().setPersistence("local")
|
|
// No user is signed in.
|
|
var provider = new firebase.auth.GoogleAuthProvider();
|
|
firebase.auth().signInWithPopup(provider).then(function(result) {
|
|
// This gives you a Google Access Token. You can use it to access the Google API.
|
|
var token = result.credential.accessToken;
|
|
// The signed-in user info.
|
|
var user = result.user;
|
|
readData("users", function(array) {
|
|
checkLogin();
|
|
});
|
|
// ...
|
|
}).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;
|
|
console.log(errorCode)
|
|
console.log(errorMessage)
|
|
// ...
|
|
});
|
|
}
|
|
|
|
// Firebase Log Out
|
|
function logOut() {
|
|
firebase.auth().signOut().then(function() {
|
|
checkLogin();
|
|
}, function(error) {
|
|
console.log("ERROR")
|
|
console.log(error)
|
|
});
|
|
}
|
|
|
|
|
|
// Firebase Interaction Scripts
|
|
function readData(ref, callback) {
|
|
var nextformation = firebase.database().ref(ref);
|
|
nextformation.on("value", function(data) {
|
|
var array = data.val();
|
|
callback(array)
|
|
});
|
|
}
|
|
// global read data and place on page function
|
|
function setData(ref, element, read1) {
|
|
var array = readData(ref, function(array) {
|
|
document.getElementById(element).innerHTML = array[read1]
|
|
});
|
|
}
|
|
|
|
// Firebase Interaction Scripts
|
|
function sendData(ref, data) {
|
|
var database = firebase.database();
|
|
firebase.database().ref(ref).set(data);
|
|
} |