127 lines
4.3 KiB
JavaScript
127 lines
4.3 KiB
JavaScript
|
|
|
|
function onLoad() {
|
|
startFirebase()
|
|
document.getElementById("main_content").style.display = "none";
|
|
document.getElementById("unauthorized").style.display = "none";
|
|
checkLogin()
|
|
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);
|
|
}
|
|
|
|
// 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("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("main_content").innerHTML = ""
|
|
document.getElementById("login_container").innerHTML = ""
|
|
document.getElementById("unauthorized").style.display = "block";
|
|
}
|
|
})
|
|
} else {
|
|
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);
|
|
} |