You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Nodemailer发送的欢迎邮件中添加Firebase密码重置链接?

Hey there! Adding a Firebase password reset link to your existing Nodemailer welcome flow is totally straightforward once you leverage Firebase's Auth SDK. Let's walk through the steps and update your code accordingly.

Step 1: Set Up Firebase Admin (If Not Already Done)

First, make sure you have the Firebase Admin SDK installed and initialized in your project. If you haven't already, run this in your terminal:

npm install firebase-admin

Then initialize it using your service account key (downloaded from the Firebase Console):

const admin = require("firebase-admin");
const serviceAccount = require("./path-to-your-service-account-key.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

Firebase handles all the token generation and expiration logic for password resets. We'll use the generatePasswordResetLink method to create a valid, secure URL for the new user.

Step 3: Update Your Email Template & Send Flow

Now integrate the generated link into your existing email template and tie it all together with Nodemailer. Here's your updated, working code:

const admin = require("firebase-admin");
const mailer = require('nodemailer');

// Initialize Firebase Admin (skip if already initialized elsewhere)
const serviceAccount = require("./path-to-your-service-account-key.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

// Your existing Nodemailer configuration
const mailConfig = {
 host: 'smtp.ethereal.email',
 port: 587,
 auth: {
 user: 'nyik6nntutmq3vz6@ethereal.email',
 pass: '3cbRjkZdPquDqA725s'
 }
};
const transporter = mailer.createTransport(mailConfig);

// Async function to send welcome email with password reset link
async function sendWelcomeEmail(userRecord) {
  try {
    // Generate the Firebase password reset link
    const resetPasswordLink = await admin.auth().generatePasswordResetLink(userRecord.email);

    // Updated HTML email template with the reset link
    const output = `
      <p>You have access to the Church Mutual Assignment Tool.</p>
      <p>Follow this link to set your account password (for ${userRecord.email}):</p>
      <a href="${resetPasswordLink}" style="padding: 8px 16px; background: #007bff; color: white; text-decoration: none; border-radius: 4px;">Set My Password</a>
      <p>Thanks,</p>
      <p>Your Church Mutual Assignment Tool team</p>
    `;

    const message = {
     from: 'nyik6nntutmq3vz6@ethereal.email',
     to: `${userRecord.email}`,
     subject: 'Welcome to the Church Mutual Assignment Tool',
     // Plaintext fallback for non-HTML email clients
     text: `You have access to the Church Mutual Assignment Tool.\n\nFollow this link to set your account password: ${resetPasswordLink}\n\nThanks,\nYour Church Mutual Assignment Tool team`,
     html: output
    };

    // Send the email
    await transporter.sendMail(message);
    console.log('Welcome email with reset link sent successfully!');
  } catch (error) {
    console.error('Error sending email or generating reset link:', error);
  }
}

// Export transporter and send function for use in your user creation logic
module.exports = { transporter, sendWelcomeEmail };

Quick Notes:

  • Async/Await: The generatePasswordResetLink method is asynchronous, so we wrap the flow in an async function and use await to ensure we have the link before building the email.
  • Plaintext Fallback: Always update the text field too—users who don't view HTML emails still need access to the reset link.
  • Error Handling: Added basic error catching to help debug issues with link generation or email sending.

Just call sendWelcomeEmail(userRecord) when you create a new user (passing the Firebase userRecord object from your user creation code) and it'll send the welcome email with the password reset link included!

内容的提问来源于stack exchange,提问作者James Delaney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:15:47