【问题标题】:How can I use the Firebase user object outside of the Firebase function?如何在 Firebase 函数之外使用 Firebase 用户对象?
【发布时间】:2021-10-15 22:24:05
【问题描述】:

我可以使用 Firebase 的 Google Auth 将用户登录到我的应用程序。但是,我似乎无法让 User 对象的任何部分出现在我的应用程序的前端(我正在使用 Pug 模板)。我怎样才能做到这一点?

这是我使用 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);
      // ...
    });
});

我希望能够在整个应用程序中引用我的 .pug 文件中的“用户”变量。现在,我有一个可以传入数据的 viewController 文件,例如:

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

有没有办法让我将用户对象从 index.js 传递到我的 viewController 中,这样它就可以在 Pug 模板中使用?我很迷茫。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript node.js firebase pug google-authentication


    【解决方案1】:

    这里的问题有两个:

    1. 您需要将变量传递给您的 pug 模板,您可以在告诉 pug 呈现模板时执行此操作(例如 here 所示)。
    2. 用户数据从 Firebase 异步加载。您需要仅在加载数据后渲染模板,或者在加载模板后重新渲染模板。

    我没有看到第二步的任何固定解决方案,但我通常会寻找一种方法来从回调中调用 pug 渲染(所以你现在有location.assign('/dashboard'))。

    另见类似问题:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-25
      • 2020-12-27
      • 2021-05-31
      • 1970-01-01
      • 2020-03-16
      • 2020-11-11
      • 2016-11-12
      • 2016-05-26
      相关资源
      最近更新 更多