【发布时间】: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