Firebase:通过云功能检查用户电子邮件是否已验证
有没有办法检查电子邮件是否通过云功能验证 i、 e.如果我有用户的uid,我可以检查电子邮件是否针对该特定用户进行了验证。在我的用例中,我需要确保在执行事务之前验证电子邮件。我想在服务器端检查一下 示例云函数:Firebase:通过云功能检查用户电子邮件是否已验证,firebase,firebase-authentication,google-cloud-functions,Firebase,Firebase Authentication,Google Cloud Functions,有没有办法检查电子邮件是否通过云功能验证 i、 e.如果我有用户的uid,我可以检查电子邮件是否针对该特定用户进行了验证。在我的用例中,我需要确保在执行事务之前验证电子邮件。我想在服务器端检查一下 示例云函数: exports.executeTransaction = functions.https.onCall((data,context)=>{ const userid = context.auth.uid //Check if email is verified //I wa
exports.executeTransaction = functions.https.onCall((data,context)=>{
const userid = context.auth.uid
//Check if email is verified
//I want to use context variable to somehow extract whether email is verified. Is there a way to do it ?
//Execute Transaction if email is verified
})
没关系,我设法弄明白了 如有类似问题,请参阅以下内容:
exports.executeTransaction = functions.https.onCall((data,context)=>{
const userid = context.auth.uid
//Check if email is verified
return admin.auth().getUser(context.auth.uid).then(user => {
//Check if email verified
if(user.emailVerified)
{
return "Verified"
}
else{
console.log("User Email not verified")
return "Not Verified"
}
}).catch(function(err){
console.log(err)
throw new functions.https.HttpsError('Error Validating', err.message, err)
})
})
根据文档,上下文包括decodedIdToken,它已经包含一个email\u验证字段 因此,您需要做的就是:
exports.executeTransaction = functions.https.onCall((data, context) => {
const { token } = context.auth;
if (!token.firebase.email_verified)
throw new functions.https.HttpsError(
"failed-precondition",
"The function must be called while authenticated."
);
// ...do stuff
})