我正在尝试使用自定义声明创建用户。我正在使用 Firebase 云功能。问题是,当我创建(注册)用户时,onCreate 不会触发。我正在关注谷歌提供的这个教程。 https://firebase.google.com/docs/auth/admin/custom-claims 我部署了我的功能,区域是 us-central1
云函数版本:
firebase-admin": "^8.9.0
firebase-functions": "^3.3.0
我使用 Vue JS 作为前端
我的函数/Index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.ProccessSignUp = functions.auth.user().onCreate(async (user) =>{
console.log("Email"+user.email);
if (user.email){
const customClaims = {
admin:true
};
return admin.auth().setCustomUserClaims(user.uid,customClaims)
.then(() =>{
const metadataRef = admin.database().ref('metadata/' +user.uid);
return metadataRef.set({refeshTime:new Date().getTime()})
}).catch(err =>{
console.log(err.message)
})
}
});
我的电子邮件和密码注册
userSignUp({dispatch},payload){
firebase.auth().createUserWithEmailAndPassword(payload.email, payload.password)
.then(user =>{
user.user.sendEmailVerification()
.then(() =>
alert('Your account has been created! Please, verify your account'),
dispatch('userSignOut'),
).catch(err =>{
console.log(err.message)
})
}).catch(err =>{
console.log(err.message)
})
},
oAuthStateChanged
router.beforeEach(async (to, from, next) => {
const user = await new Promise((resolve) => {
firebase.auth().onAuthStateChanged(async user => {
await store.dispatch("autoSignIn", user);
resolve(user)
});
});
const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
if (requiresAuth) {
if (!user){
next(false)
}else {
if (user.emailVerified){
next();
}else {
alert('Please verify your account')
await store.dispatch("userSignOut", user);
}
}
} else {
next()
}
});
最佳答案
如 doc 中所述,使用 Cloud Functions,您可以“从函数中发出日志行,使用标准 JavaScript 日志记录调用,例如 console.log
和 console.error
”。
然后,可以在 Firebase 控制台、Stackdriver Logging UI 或通过 firebase 命令行工具查看 Cloud Functions 日志。
因此,您应该能够通过查看 Firebase 控制台等来确认您的 Cloud Function 是否正确运行。
关于javascript - Firebasefunctions.auth.user().onCreate不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61152967/