[SOLVED] Firebase Help (returning out of a window.function)

I’ve looked over docs and the forums and I can’t seem to find anyone detailed firebase Playcanvas implementations. If anyone has a pre built one or just some level of guidance please help.

Thanks.
Sincerely,
William Boersma

Hi @WilliamBoersma31,

I don’t have an example in mind, but this forum post is quite detailed: Is there an example of a database connection with PlayCanvas? - #2 by Leonidas

2 Likes

Hey @Leonidas I’ve read over the forum and I’ve got the two scripts amd I’ve added a function but every time I try to use it I get the following error…

ASSERT FAILED:
script [undefined] has missing arguments in constructor

Do you have any ideas on how to fix this?

update I got rid of the error it just won’t run the imported functions. For example, I’ve set up a simple log in function that just takes in an email and password and runs signInWithEmailAndPassword the function runs but doesn’t run the previously mentioned imported firebase function. Any ideas @Leonidas ?

Sorry about the spam updates but I’m just here debugging. Anyways I’ve got the error message in a console.log but it’s not going through the return errorMessage; any ideas on what could cause this @Leonidas ? Edit: The error given was this…
“Cannot read properties of undefined (reading ‘tenantId’)” Edit Again: If I test with correct info it works as intended but if I test with wrong info it simply won’t send back the error for some reason the previously mentioned return just won’t work. Here is my code as of writting…

    window.LogIn = function (email, password) {
      console.log("Logging In With Email: " + email + ", Password: " + password);
       signInWithEmailAndPassword(window.auth, email, password)
  .then((userCredential) => {
    // Signed in 
    const user = userCredential.user;
    console.log("Logged In As " + user.uid);
           localStorage.setItem("LoggedIn", true);
          localStorage.setItem("email", email);
          localStorage.setItem("password", password);
         setTimeout(function () {
           return null;
         }, 500);
        
        
  
  })
  .catch((error) => {
    const errorMessage = error.message;
    //console.log(errorMessage);
            var NewEM = errorMessage.replace("Firebase:", " ");
            console.log(NewEM);
            setTimeout(function() {
return NewEM;
            }, 500); 
  });
            };

And the code to call it…

function upd () {
  var er = window.LogIn(this.Email.script.input.val, this.Password.script.input.val);
  console.log("er:" + er);
  if (er == null) {
//this.Screen.enabled = false;
  }else{
    this.er.element.text = er;
  }
}

@Leonidas ?

Never mind I’ve figured it out on my own. For anyone wondering the solution is just to make the return data it’s own window. variable and then run off that.

1 Like

Hey @Leonidas while I’m here I have a different yet related issue.
It was working until it randomly started saying…

" @firebase/auth: Auth (9.13.0): INTERNAL ASSERTION FAILED: Could not find fetch implementation, make sure you call FetchProvider.initialize() with an appropriate polyfill"

upon every launch. I tried everything including just restarting and going of my pervious solution but it stills throws that same error. Any ideas?

Update: I’ve tracked it down to something about the
signInWithEmailAndPassword()
function as it doesn’t throw the error unless that function is run.

@WilliamBoersma31 I haven’t really been following this thread very closely but, In the authentication area of your firebase settings do you have both of these or just email?

I am not sure what your steps are for authenticating. Are you just using the API key method or?

Hey @Tirk182 I have just email. For authenticating I simply run the signInWithEmailAndPassword function I’ve mentioned previously. Here is a snippet of my code

 signInWithEmailAndPassword(auth, email, password)
  .then((userCredential) => {
    // Signed in 
    const user = userCredential.user;
           localStorage.setItem("LoggedIn", true);
          localStorage.setItem("email", email);
          localStorage.setItem("password", password);
         setTimeout(function () {
           window.NewEM = "Success";
         }, 500);
        
        
  
  })
  .catch((error) => {
         const errorMessage = error.message;
            var NewEM = errorMessage.replace("Firebase:", " ");
            window.NewEM = NewEM;
            console.log(NewEM);
  });

Would adding anonymous fix this? I’ve never had to have it when I set up authentication on my site.

@WilliamBoersma31 I am not totally sure because I don’t have a real clear picture on how you are authenticating. I mean I can see some of it in the code above. So when you have a logon occur what do you see in the database Auth area here?

image

As part of the firebase hosting front end you should also have some default login dialogs from Google that you can use for the process. I would try anonymous. I have taken a look at that error on the Web. It may be just the way you are doing the sign in but, I just can’t see enough to tell. You also have logs you can look at online to see what is happening. Are you also doing hosting as well?

Hey @Tirk182 I’ve tried with anonymous enabled and still got the same error. This is strange as it didn’t occur just last night but now I can’t avoid it. It also doesn’t pop up on the site which uses practically the same code. I’ve mainly just copied from there and then modified the outputs to fit this situation. Anyways here is a better look at my code…

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.13.0/firebase-app.js";
  import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.13.0/firebase-analytics.js";
  // TODO: Add SDKs for Firebase products that you want to use
  // https://firebase.google.com/docs/web/setup#available-libraries

  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  const firebaseConfig = {
(Sensitive Info)
  };

  // Initialize Firebase
  const app = initializeApp(firebaseConfig);
  const analytics = getAnalytics(app);

  import { getAuth, signInWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/9.13.0/firebase-auth.js";

const auth = getAuth(app);

  import { getDatabase, ref, set, onValue, update, push, child} from "https://www.gstatic.com/firebasejs/9.13.0/firebase-database.js";

    const db = getDatabase();

    window.LogIn = function (email, password) {
      if (app != null && auth != null) {
      console.log("Logging In With Email: " + email + ", Password: " + password);
//console.log(auth);
     signInWithEmailAndPassword(auth, email, password)
  .then((userCredential) => {
    // Signed in 
    const user = userCredential.user;
           localStorage.setItem("LoggedIn", true);
          localStorage.setItem("email", email);
          localStorage.setItem("password", password);
         setTimeout(function () {
           window.NewEM = "Success";
         }, 500);
        
        
  
  })
  .catch((error) => {
         const errorMessage = error.message;
            var NewEM = errorMessage.replace("Firebase:", " ");
            window.NewEM = NewEM;
            console.log(NewEM);
  });
      }else{
        console.error("Oh No!");
      }
            };

      

It’s then called from a seperate script just running

window.LogIn(EmailLocation, PasswordLocation);

@WilliamBoersma31 I don’t have much more information this evening sorry. Did google update the SDK by any chance? They are famous for this. Please check and make sure you have all of the latest. Also, this method you are using, Is it something you have tested before? Or Is this an example somewhere you can point me to? Reading through the Google docs and from what I have experienced before is that something doesn’t look correct. Maybe if you can point me to where you have found this I can have a look.