152 lines
5.8 KiB
JavaScript
152 lines
5.8 KiB
JavaScript
|
|
|
|
function onLoad() {
|
|
startFirebase()
|
|
document.getElementById("main_content").style.display = "none";
|
|
document.getElementById("unauthorized").style.display = "none";
|
|
checkLogin()
|
|
displayProjects("all")
|
|
setInterval(function() {
|
|
checkLogin();
|
|
}, 1000);
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
function displayProjects(status) {
|
|
document.getElementById("projects").innerHTML = ""
|
|
database = firebase.database().ref("projects");
|
|
database.once('value', function (snapshot) {
|
|
snapshot.forEach(function (child) {
|
|
firebase.database().ref("projects/" + child["key"]).on('value', function (data) {
|
|
var data1 = data.val();
|
|
var a = document.createElement("div")
|
|
var title = "'"+data1["title"]+"'"
|
|
a.innerHTML = '<div class="project-card mdl-card mdl-shadow--2dp"> <div class="mdl-card__title mdl-card--expand"> <h2 class="mdl-card__title-text">' + data1["title"].replace(/,/g, ".") + '<br>' + data1["status"] + '</h2> </div> <div class="mdl-card__supporting-text">' + data1["description"] + ' </div> <div class="mdl-card__actions mdl-card--border"> <a href="javascript:editProject(' + title + ')" class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect"> Edit </a> </div> </div>'
|
|
|
|
if (status === "all") {
|
|
document.getElementById("projects").appendChild(a)
|
|
} else if (data1["status"] === status) {
|
|
document.getElementById("projects").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"
|
|
}
|
|
})
|
|
}
|
|
|
|
// 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("loading").style.display = "none";
|
|
document.getElementById("firebase_name").innerText = firebase.auth().currentUser.displayName
|
|
document.getElementById("firebase_email").innerText = firebase.auth().currentUser.email
|
|
document.getElementById("firebase_photo").setAttribute("src", firebase.auth().currentUser.photoURL)
|
|
document.getElementById("main_content").style.display = "block";
|
|
document.getElementById("login_container").style.display = "none";
|
|
document.getElementById("unauthorized").style.display = "none";
|
|
} else {
|
|
document.getElementById("loading").style.display = "none";
|
|
document.getElementById("main_content").innerHTML = ""
|
|
document.getElementById("login_container").innerHTML = ""
|
|
document.getElementById("unauthorized").style.display = "block";
|
|
}
|
|
})
|
|
} else {
|
|
document.getElementById("loading").style.display = "none";
|
|
document.getElementById("main_content").style.display = "none";
|
|
document.getElementById("login_container").style.display = "block";
|
|
document.getElementById("unauthorized").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);
|
|
} |