如何知道云函数中firebase身份验证的提供者

如何知道云函数中firebase身份验证的提供者,firebase,firebase-authentication,google-cloud-functions,Firebase,Firebase Authentication,Google Cloud Functions,我正在使用firebase云功能向用户发送欢迎电子邮件,当他们为应用程序创建新帐户时。只有当用户使用emailAndPassword身份验证创建电子邮件时,我才需要发送此欢迎电子邮件,因此我需要知道用户的身份验证提供商。这是我现在的代码: const gmailEmail = functions.config().gmail.email; const gmailPassword = functions.config().gmail.password; const mailTranspor

我正在使用firebase云功能向用户发送欢迎电子邮件,当他们为应用程序创建新帐户时。只有当用户使用emailAndPassword身份验证创建电子邮件时,我才需要发送此欢迎电子邮件,因此我需要知道用户的身份验证提供商。这是我现在的代码:

    const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: gmailEmail,
    pass: gmailPassword,
  },
});

// Your company name to include in the emails
// TODO: Change this to your app or company name to customize the email sent.
const APP_NAME = 'العب .. تعلم';

// [START sendWelcomeEmail]
/**
 * Sends a welcome email to new user.
 */
// [START onCreateTrigger]
exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
// [END onCreateTrigger]
  // [START eventAttributes]
  const email = user.email; // The email of the user.
  const displayName = user.displayName; // The display name of the user.
  // [END eventAttributes]

  return sendWelcomeEmail(email, displayName);
});
// [END sendWelcomeEmail]



// [START sendByeEmail]
/**
 * Send an account deleted email confirmation to users who delete their accounts.
 */
/* TODO :remove this comment to add goodbye email
// [START onDeleteTrigger]
exports.sendByeEmail = functions.auth.user().onDelete((user) => {
// [END onDeleteTrigger]
  const email = user.email;
  const displayName = user.displayName;

  return sendGoodbyeEmail(email, displayName);
});
// [END sendByeEmail]


*/

// Sends a welcome email to the given user.
function sendWelcomeEmail(email, displayName) {
  const mailOptions = {
    from: `${APP_NAME} <noreply@firebase.com>`,
    to: email,
  };

  // The user subscribed to the newsletter.
  mailOptions.subject = `welcome in our app! `;
  const startText = `Welcome in our app! We hope you enjoy it. To know the latest news about the app and the latest competitions please join the following facebook page : `;
  const groupLink = `https://www.facebook.com/2057244580979539/`;
  mailOptions.text = startText + `\n\n` + groupLink;//TODO : add new line instead of space
  return mailTransport.sendMail(mailOptions).then(() => {
    return console.log('New welcome email sent to:', email);
  });
}
此代码将向在应用程序中创建电子邮件的任何用户发送欢迎电子邮件。只有当用户使用emailAndPasswordProvider在应用程序中创建新电子邮件时,我才需要向用户发送电子邮件。

您有两个选项: 您可以检查用户记录提供程序数据数组。每个条目都有一个providerId:

但是,上述内容对于电子邮件链接登录以及电子邮件/密码也具有相同的值

另一个选项是使用client Node.js SDK并调用:

firebase.auth().fetchSignInMethodsForEmail(user.email)
  .then((signInMethods) => {
    if (signInMethods.indexOf('password') !== -1) {
      // Email corresponds to email/password user.
      // Email link user will have 'emailLink' in the array.
    }
  })

当他们第一次创建用户时,向其详细信息中添加一个值,您可以稍后选择该值以确认他们使用的身份验证类型
firebase.auth().fetchSignInMethodsForEmail(user.email)
  .then((signInMethods) => {
    if (signInMethods.indexOf('password') !== -1) {
      // Email corresponds to email/password user.
      // Email link user will have 'emailLink' in the array.
    }
  })