feat: new secrets

This commit is contained in:
2026-07-20 15:23:16 +02:00
parent 5a6833c1a2
commit 3fff09ba81
18 changed files with 121 additions and 109 deletions
+18 -10
View File
@@ -6,14 +6,26 @@ const { ORDER_TYPES, createOrderDocument } = require('./helpers/orders')
const { deleteFolder } = require('../helpers/firebase')
const { Resend } = require('resend')
const { welcomeTemplate } = require('../helpers/email')
const { RESEND_API_KEY } = require('../config/keys')
const { RESEND_API_KEY } = require('../config/secrets')
const { onRequest } = require('firebase-functions/https')
const resendClient = new Resend(RESEND_API_KEY)
let resendClient = null
const getResendClient = () => {
if (!resendClient) {
resendClient = new Resend(RESEND_API_KEY.value())
}
return resendClient
}
const WELCOME_EMAIL_FROM = 'MusicLand <musicland@musicland.ai>'
const WELCOME_EMAIL_SUBJECT = 'Bienvenue sur MusicLand'
const USER_CREATED_OPTIONS = {
document: 'users/{userID}',
secrets: [RESEND_API_KEY],
}
exports.testWelcomMail = onRequest(async (req, res) => {
exports.testWelcomMail = onRequest({ secrets: [RESEND_API_KEY] }, async (req, res) => {
if (req.method !== 'GET') {
res.set('Allow', 'GET')
return res.status(405).json({ success: false, error: 'Method not allowed' })
@@ -22,7 +34,7 @@ exports.testWelcomMail = onRequest(async (req, res) => {
const targetEmail = req.query.email || 'tdtomthomas@gmail.com'
const firstName = req.query.firstName || 'Toto'
const lastName = req.query.lastName || 'Test'
const { data, error } = await resendClient.emails.send({
const { data, error } = await getResendClient().emails.send({
from: WELCOME_EMAIL_FROM,
to: [targetEmail],
subject: WELCOME_EMAIL_SUBJECT,
@@ -40,7 +52,7 @@ exports.testWelcomMail = onRequest(async (req, res) => {
}
})
exports.onUserCreated = onDocumentCreated('users/{userID}', async (event) => {
exports.onUserCreated = onDocumentCreated(USER_CREATED_OPTIONS, async (event) => {
try {
const { email = '', firstName = '', lastName = '' } = event?.data?.data() || {}
@@ -62,13 +74,9 @@ exports.onUserCreated = onDocumentCreated('users/{userID}', async (event) => {
)
}
if (email) {
if (!resendClient) {
console.warn('Resend API key not configured; skipping welcome email.')
return
}
try {
console.log(`Sending welcome email to ${email}`)
const { data, error } = await resendClient.emails.send({
const { data, error } = await getResendClient().emails.send({
from: WELCOME_EMAIL_FROM,
to: [email],
subject: WELCOME_EMAIL_SUBJECT,