Firebase:检查云功能是否验证了用户电子邮件

时间:2018-07-03 05:35:20

标签: firebase firebase-authentication google-cloud-functions

是否可以检查是否通过云功能验证了电子邮件 即,如果我有该用户的uid,可以检查该特定用户的电子邮件是否经过验证。在我的用例中,我需要确保在执行交易之前已验证电子邮件。我要在服务器端进行检查

样本云功能:

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
})

2 个答案:

答案 0 :(得分:1)

没关系,我设法弄清楚了。

遇到类似问题的人,请参见以下内容:

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)
})
})

答案 1 :(得分:0)

根据文档,上下文包括decodedIdToken,该文本已经包含email_verified字段。

因此,您所需要做的就是:

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
})

https://firebase.google.com/docs/reference/functions/functions.https#.CallableContext

https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken#email_verified