tra-analysis/website/public/scripts.js

227 lines
8.1 KiB
JavaScript
Raw Normal View History

2019-01-06 18:54:08 +00:00
// Initialize Firebase
var config = {
apiKey: "(insert the TitanScout Api Key Here)",
2019-01-06 18:54:08 +00:00
authDomain: "titanscoutandroid.firebaseapp.com",
databaseURL: "https://titanscoutandroid.firebaseio.com",
projectId: "titanscoutandroid",
storageBucket: "titanscoutandroid.appspot.com",
messagingSenderId: "1097635313476"
};
firebase.initializeApp(config);
// FirebaseUI config.
var uiConfig = {
2019-02-06 20:51:31 +00:00
callbacks: {
signInSuccessWithAuthResult: function(authResult, redirectUrl) {
2019-01-06 18:54:08 +00:00
if (authResult.user) {
2019-02-06 21:37:27 +00:00
handleSignedInUser(authResult.user);
2019-01-06 18:54:08 +00:00
}
return false;
}
},
//signInSuccessUrl: '<url-to-redirect-to-on-success>',
signInOptions: [
// Leave the lines as is for the providers you want to offer your users.
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
//firebase.auth.FacebookAuthProvider.PROVIDER_ID,
//firebase.auth.TwitterAuthProvider.PROVIDER_ID,
firebase.auth.GithubAuthProvider.PROVIDER_ID,
firebase.auth.EmailAuthProvider.PROVIDER_ID,
firebase.auth.PhoneAuthProvider.PROVIDER_ID,
2019-02-06 20:51:31 +00:00
// firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID
2019-01-06 18:54:08 +00:00
],
// tosUrl and privacyPolicyUrl accept either url string or a callback
// function.
// Terms of service url/callback.
2019-02-06 20:51:31 +00:00
tosUrl: function() {
alert("this is a test app. don't use it");
},
2019-01-06 18:54:08 +00:00
// Privacy policy url/callback.
privacyPolicyUrl: function() {
alert("we will steal all of the data");
}
};
var handleSignedInUser = function(user) {
2019-02-06 23:51:58 +00:00
document.getElementById("mainhead").innerHTML = "TitanScout- User Info";
2019-02-06 20:51:31 +00:00
if (user.displayName != null) {
2019-01-06 18:54:08 +00:00
document.getElementById('status').innerHTML = "You are signed in as: " + user.displayName;
2019-02-06 20:51:31 +00:00
} else if (user.email != null) {
2019-01-06 18:54:08 +00:00
document.getElementById('status').innerHTML = "You are signed in as: " + user.email;
2019-02-06 20:51:31 +00:00
} else if (user.phoneNumber != null) {
2019-01-06 18:54:08 +00:00
document.getElementById('status').innerHTML = "You are signed in as: " + user.phoneNumber;
2019-02-06 20:51:31 +00:00
} else {
2019-01-06 18:54:08 +00:00
document.getElementById('status').innerHTML = "You are signed in.";
}
2019-02-06 20:51:31 +00:00
document.getElementById('signout').style.display = 'inline-block';
document.getElementById('updpi').style.display = 'inline-block';
document.getElementById('deleteacc').style.display = 'inline-block';
document.getElementById('profileupd').style.display = 'none';
2019-02-06 22:14:39 +00:00
document.getElementById('sideload').style.display = 'block';
2019-01-06 18:54:08 +00:00
}
var handleSignedOutUser = function() {
document.getElementById("mainhead").innerHTML = "TitanScout- Sign In";
document.getElementById('status').innerHTML = "You are not signed in.";
2019-02-06 20:51:31 +00:00
document.getElementById('signout').style.display = 'none';
document.getElementById('updpi').style.display = 'none';
document.getElementById('deleteacc').style.display = 'none';
document.getElementById('profileupd').style.display = 'none';
2019-02-06 22:14:39 +00:00
document.getElementById('sideload').style.display = 'none';
2019-01-06 18:54:08 +00:00
ui.start('#firebaseui-auth-container', uiConfig);
};
// Initialize the FirebaseUI Widget using Firebase.
var ui = new firebaseui.auth.AuthUI(firebase.auth());
// The start method will wait until the DOM is loaded.
ui.start('#firebaseui-auth-container', uiConfig);
var deleteAccount = function() {
try {
firebase.auth().currentUser.delete()
handleSignedOutUser()
} catch (error) {
if (error.code == 'auth/requires-recent-login') {
// The user's credential is too old. She needs to sign in again.
signout()
2019-02-06 20:51:31 +00:00
// The timeout allows the message to be displayed after the UI has
// changed to the signed out state.
2019-01-06 18:54:08 +00:00
setTimeout(function() {
2019-02-06 20:51:31 +00:00
alert('Please sign in again to delete your account.');
}, 1);
}
2019-01-06 18:54:08 +00:00
}
};
2019-02-06 20:51:31 +00:00
2019-01-06 18:54:08 +00:00
function signout() {
var user = firebase.auth().currentUser;
firebase.auth().signOut()
handleSignedOutUser()
}
2019-02-06 20:51:31 +00:00
function loadupdpi() {
if (firebase.auth().currentUser != null) {
document.getElementById('profileupd').style.display = 'block';
} else {
2019-02-06 19:24:56 +00:00
setTimeout(function() {
2019-02-06 20:51:31 +00:00
alert('Please sign in to change your account info.');
}, 1);
2019-02-06 19:57:29 +00:00
handleSignedOutUser();
2019-02-06 19:24:56 +00:00
}
}
2019-02-06 20:51:31 +00:00
2019-02-06 20:46:41 +00:00
function upProfileInfo() {
2019-02-06 20:51:31 +00:00
if (firebase.auth().currentUser != null) {
2019-02-06 21:25:15 +00:00
var user = firebase.auth().currentUser;
2019-02-06 21:22:09 +00:00
var newDN = document.getElementById('newDN').value;
var newEM = document.getElementById('newEM').value;
var newPP = document.getElementById('newPP').value;
var si = true
if (newDN != '' && newDN != user.displayName) {
if (newPP != '' && newPP != user.photoURL) {
try {
2019-02-06 20:51:31 +00:00
user.updateProfile({
2019-02-06 21:22:09 +00:00
displayName: newDN,
photoURL: newPP
2019-02-06 20:51:31 +00:00
});
2019-02-06 21:22:09 +00:00
} catch (error) {
if (error.code == 'auth/requires-recent-login') {
si = false;
// The user's credential is too old. She needs to sign in again.
signout()
// The timeout allows the message to be displayed after the UI has
// changed to the signed out state.
setTimeout(function() {
alert('Please sign in again to delete your account.');
}, 1);
2019-02-06 20:51:31 +00:00
} else {
2019-02-06 21:22:09 +00:00
alert("An error occurred: " + error)
2019-02-06 20:51:31 +00:00
}
}
2019-02-06 21:22:09 +00:00
} else {
try {
user.updateProfile({
displayName: newDN
});
} catch (error) {
if (error.code == 'auth/requires-recent-login') {
si = false;
// The user's credential is too old. She needs to sign in again.
signout()
// The timeout allows the message to be displayed after the UI has
// changed to the signed out state.
setTimeout(function() {
alert('Please sign in again to delete your account.');
}, 1);
2019-02-06 20:51:31 +00:00
} else {
2019-02-06 21:22:09 +00:00
alert("An error occurred: " + error)
2019-02-06 20:51:31 +00:00
}
2019-02-06 21:22:09 +00:00
}
}
} else {
if (newPP != '' && newPP != user.photoURL) {
try {
user.updateProfile({
photoURL: newPP
});
} catch (error) {
2019-02-06 20:51:31 +00:00
if (error.code == 'auth/requires-recent-login') {
2019-02-06 21:22:09 +00:00
si = false;
2019-02-06 20:51:31 +00:00
// The user's credential is too old. She needs to sign in again.
signout()
// The timeout allows the message to be displayed after the UI has
// changed to the signed out state.
setTimeout(function() {
2019-02-06 21:22:09 +00:00
alert('Please sign in again to delete your account.');
2019-02-06 20:51:31 +00:00
}, 1);
} else {
2019-02-06 21:22:09 +00:00
alert("An error occurred: " + error)
2019-02-06 20:51:31 +00:00
}
2019-02-06 21:22:09 +00:00
}
}
}
if (newEM != '' && newEM != user.email) {
try {
user.updateEmail(newEM)
} catch (error) {
si = false;
if (error.code == 'auth/requires-recent-login') {
// The user's credential is too old. She needs to sign in again.
signout()
// The timeout allows the message to be displayed after the UI has
// changed to the signed out state.
setTimeout(function() {
alert('Please sign in again to delete your account.');
}, 1);
2019-02-06 20:51:31 +00:00
} else {
2019-02-06 21:22:09 +00:00
alert("An error occurred: " + error)
2019-02-06 20:51:31 +00:00
}
2019-02-06 20:46:41 +00:00
}
2019-02-06 20:51:31 +00:00
}
2019-02-06 21:22:09 +00:00
if (si) {
2019-02-06 21:55:22 +00:00
setTimeout(function(){handleSignedInUser(user);},1)
2019-02-06 21:22:09 +00:00
}
} else {
2019-02-06 20:46:41 +00:00
setTimeout(function() {
2019-02-06 20:51:31 +00:00
alert('Please sign in to change your account info.');
}, 1);
2019-02-06 21:37:27 +00:00
handleSignedOutUser();
2019-02-06 20:46:41 +00:00
}
}
2019-02-06 22:08:28 +00:00
/* Set the width of the side navigation to 250px and the left margin of the page content to 250px and add a black background color to body */
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
/* Set the width of the side navigation to 0 and the left margin of the page content to 0, and the background color of body to white */
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
}
2019-02-07 00:19:18 +00:00
window.onload=function{
if(firebase.auth().currentUser!=null){
handleSignedInUser(firebase.auth().currentUser)
}
2019-02-06 23:51:58 +00:00
}