0

I am able to log users into my application using Google Auth from Firebase. However, I can't seem to get any part of the User object to appear on the front end of my application (I'm using Pug templates). How can I achieve this?

This is the code that I am using to sign the user into my web app using Google Auth:

document.getElementById('LogInButton').addEventListener('click', (e) => {
  signInWithPopup(auth, provider)
    .then((result) => {
      // This gives you a Google Access Token. You can use it to access the Google API.
      const credential = GoogleAuthProvider.credentialFromResult(result);
      const token = credential.accessToken;
      // The signed-in user info.
      const user = result.user;
      // ...
      window.setTimeout(() => {
        location.assign('/dashboard');
      }, 1500);
    })
    .catch((error) => {
      // Handle Errors here.
      const errorCode = error.code;
      const errorMessage = error.message;
      // The email of the user's account used.
      const email = error.email;
      // The AuthCredential type that was used.
      const credential = GoogleAuthProvider.credentialFromError(error);
      // ...
    });
});

I would like to be able to reference the "user" variable in my .pug files throughout the application. Right now, I have a viewController file where I can pass in data, for example:

exports.getSignIn = (req, res) => {
  res.status(200).render('signin', {
    title: `Sign in`,
  });
};

Is there a way for me to pass the user object from index.js into my viewController so it's available in the Pug template? I'm pretty lost. Any help would be appreicated.

Chris Clark
  • 488
  • 2
  • 19

1 Answers1

1

The problem here is two-fold:

  1. You need to pass the variable to your pug template, which you can do when you tell pug to render the template (as shown for example here).
  2. User data is loaded from Firebase asynchronously. You need to either only render the template once the data is loaded, or re-render the template when it has loaded.

I don't see any canned solutions for this second step, but I'd typically look for a way to call the pug rendering from within the callback (so where you now have location.assign('/dashboard')).

Also see this similar question:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807