We're on Discord! Please join our server now if you don't want to miss anything! (More info) | Join the UCC4 contest! (More info)

* WalrusIRC

You need to have 5 posts and not be part of restricted usergroups in order to use the WalrusIRC embedded shoutbox. However, you can also access our IRC channel called #CodeWalrus via EFnet.

Author Topic: 123outerme's Minor Firebase Projects  (Read 546 times)

0 Members and 1 Guest are viewing this topic.

Offline 123outerme

  • Super User
  • Join Date: Jan 2015
  • Location: In spaaace! Well, the Earth is anyhow.
  • Posts: 722
  • Post Rating Ratio: +6/-0
  • Professional Unpaid Intern
    • @UCx1XQoxAm-ZTz-lwyUqbs0w
    • 123outerme
    • 113/11355
  • Gender: Male
123outerme's Minor Firebase Projects
« on: May 09, 2018, 08:39:57 pm »
Recently I have started to learn how to user Firebase, thanks to exposure from @_iPhoenix_ . I created a test website, which I have been developing on here. I've added a login/out button, file upload and download, and database read/write (demonstrated by the "load files" button). I intend to add more features to the test website before I call it complete, but know that some pretty cool websites will most likely begin development after this testing phase :)
« Last Edit: May 09, 2018, 09:24:01 pm by 123outerme »


  • Calculators owned: TI-84+CSE, TI-nspire Clickpad, TI-84+SE

Offline 123outerme

  • Super User
  • Join Date: Jan 2015
  • Location: In spaaace! Well, the Earth is anyhow.
  • Posts: 722
  • Post Rating Ratio: +6/-0
  • Professional Unpaid Intern
    • @UCx1XQoxAm-ZTz-lwyUqbs0w
    • 123outerme
    • 113/11355
  • Gender: Male
Re: 123outerme's Minor Firebase Projects
« Reply #1 on: May 27, 2018, 06:25:28 pm »
Recently, I had trouble with login/out. When I logged out and refreshed, the user wouldn't stay logged out. When you logged into a different account, sometimes the user who was previously logged in would be the one who was logged in, when refreshing. Essentially, the user would be "remembered" and not allowed to change, even upon forcing a log out. This is the login.js file I used to control auth. gLogin() is called when the user presses Login, gLogout() is called when the user presses Logout, and that's about it.
Code: [Select]
var signedIn;
var curUser = null;

function updateUser(user)
{
if (user !== null) {
  curUser = user;
    signedIn = true;
  console.log("User is logged in");
    // User is signed in.
    } else {
curUser = null;
    signedIn = false;
    console.log("User is not logged in");
  // No user is signed in.
    }
    setButtons(signedIn);
}

firebase.auth().onAuthStateChanged(function(user) {
console.log(user);
updateUser(user);
});

function gLogin() {
  firebase.auth().signInWithPopup(gProvider).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.
    curUser = result.user;
signedIn = true;
//location.reload(true);
updateUser(curUser);
//setButtons(signedIn);
  }).catch(function (error) {
    console.log(error);
    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;
    // ...
  });
}

function gLogout() {
firebase.auth().signOut().then(function () {
signedIn = false;
updateUser(null);
location.reload();
//setButtons(signedIn);
}).catch(function (error) {
    console.log(error);
});
}

function setButtons(loggedIn) {
  var stylesheet = getStylesheet("visibility");
  if (loggedIn) {
    document.getElementById("signin").setAttribute("style", "display: none;");
    document.getElementById("signout").setAttribute("style", "display: block;");
    document.getElementById("signoutButton").innerHTML = "Logout, " + curUser.displayName;
stylesheet.deleteRule(0);
stylesheet.insertRule(".signedin { display: block; }", 0);

  } else {
    document.getElementById("signin").setAttribute("style", "display: block;");
    document.getElementById("signout").setAttribute("style", "display: none;");
stylesheet.deleteRule(0);
stylesheet.insertRule(".signedin { display: none; }", 0);
  }
}

Eventually, I figured it out: Apparently, the issue was that I didn't set a default auth persistence state. From what I understand, this meant that persistence would have undocumented behavior. Here's what I added to each of my pages' scripts:
Code: [Select]
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION).then(function() {
console.log("Persistence set");
}).catch(function(error) {
console.log(error);
});
  • Calculators owned: TI-84+CSE, TI-nspire Clickpad, TI-84+SE

 


You can also use the following HTML or bulletin board code to share it on your page or forum signature!


Also do not forget to check our affiliates below.
Planet Casio TI-Planet Calc.news BroniesQC BosaikNet Velocity Games