首页 > 解决方案 > Firebase:通过云功能检查用户电子邮件是否经过验证

问题描述

有没有办法检查电子邮件是否通过云功能验证,即如果我有用户的 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
})

标签: firebasefirebase-authenticationgoogle-cloud-functions

解决方案


根据文档,上下文包括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


推荐阅读