v0.1.61 - Resend 메일 발송 연결

This commit is contained in:
2026-04-23 17:18:11 +09:00
parent 8a46c507e9
commit d59795b089
5 changed files with 110 additions and 0 deletions

View File

@@ -14,6 +14,9 @@ const envSchema = z.object({
CORS_ORIGIN: z.string().default('http://localhost:5173'),
SESSION_TTL_DAYS: z.coerce.number().default(30),
APP_BASE_URL: z.string().default('http://localhost:5173'),
RESEND_API_KEY: z.string().optional(),
MAIL_FROM_EMAIL: z.string().email().default('planner@sori.studio'),
MAIL_FROM_NAME: z.string().default('10 Minute Planner'),
ADMIN_ACCOUNT_ID: z.string().min(1),
ADMIN_ACCOUNT_PASSWORD: z.string().min(12),
ADMIN_ACCOUNT_EMAIL: z.string().email(),

89
backend/src/lib/mailer.js Normal file
View File

@@ -0,0 +1,89 @@
import { env } from '../config.js'
const RESEND_API_URL = 'https://api.resend.com/emails'
function getFromAddress() {
return `${env.MAIL_FROM_NAME} <${env.MAIL_FROM_EMAIL}>`
}
async function sendWithResend({ to, subject, html, text }) {
if (!env.RESEND_API_KEY) {
return {
skipped: true,
reason: 'RESEND_API_KEY is not configured.',
}
}
const response = await fetch(RESEND_API_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${env.RESEND_API_KEY}`,
'Content-Type': 'application/json',
'User-Agent': 'ten-minute-planner/1.0',
},
body: JSON.stringify({
from: getFromAddress(),
to,
subject,
html,
text,
}),
})
const data = await response.json().catch(() => ({}))
if (!response.ok) {
throw new Error(data.message || data.error?.message || 'Resend 메일 발송에 실패했습니다.')
}
return {
skipped: false,
id: data.id,
}
}
function buildLinkHtml({ title, description, linkUrl, buttonLabel }) {
return `
<div style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; color: #1c1917; line-height: 1.6;">
<p style="font-size: 12px; letter-spacing: 0.18em; text-transform: uppercase; color: #78716c;">10 Minute Planner</p>
<h1 style="font-size: 24px; margin: 12px 0;">${title}</h1>
<p style="font-size: 15px; color: #57534e;">${description}</p>
<p style="margin: 24px 0;">
<a href="${linkUrl}" style="display: inline-block; border-radius: 999px; background: #1c1917; color: #ffffff; padding: 12px 18px; font-size: 13px; font-weight: 700; text-decoration: none;">
${buttonLabel}
</a>
</p>
<p style="font-size: 12px; color: #78716c;">버튼이 열리지 않으면 아래 링크를 복사해서 브라우저에 붙여넣어 주세요.</p>
<p style="font-size: 12px; word-break: break-all; color: #57534e;">${linkUrl}</p>
<p style="font-size: 12px; color: #a8a29e;">이 링크는 30분 동안 사용할 수 있습니다.</p>
</div>
`
}
export function sendVerificationEmail({ to, linkUrl }) {
return sendWithResend({
to,
subject: '[10 Minute Planner] 이메일 인증',
html: buildLinkHtml({
title: '이메일 인증을 완료해 주세요.',
description: '아래 버튼을 눌러 10 Minute Planner 계정의 이메일 인증을 완료할 수 있습니다.',
linkUrl,
buttonLabel: '이메일 인증하기',
}),
text: `10 Minute Planner 이메일 인증 링크입니다.\n${linkUrl}\n이 링크는 30분 동안 사용할 수 있습니다.`,
})
}
export function sendPasswordResetEmail({ to, linkUrl }) {
return sendWithResend({
to,
subject: '[10 Minute Planner] 비밀번호 재설정',
html: buildLinkHtml({
title: '비밀번호를 재설정해 주세요.',
description: '아래 버튼을 눌러 새 비밀번호를 설정할 수 있습니다. 요청하지 않았다면 이 메일은 무시해 주세요.',
linkUrl,
buttonLabel: '비밀번호 재설정',
}),
text: `10 Minute Planner 비밀번호 재설정 링크입니다.\n${linkUrl}\n요청하지 않았다면 이 메일은 무시해 주세요.`,
})
}

View File

@@ -4,6 +4,7 @@ import { db } from '../db/client.js'
import { authSessions, emailVerificationTokens, passwordResetTokens, users } from '../db/schema.js'
import { createSessionToken, hashSessionToken, hashPassword, verifyPassword } from '../lib/password.js'
import { createSession, findAuthenticatedUser } from '../lib/authSession.js'
import { sendPasswordResetEmail, sendVerificationEmail } from '../lib/mailer.js'
import { env } from '../config.js'
const signupSchema = z.object({
@@ -171,6 +172,10 @@ export async function registerAuthRoutes(app) {
const { token } = await createSession(user.id)
const verification = await createEmailVerificationToken(user.id)
await sendVerificationEmail({
to: user.email,
linkUrl: verification.previewUrl,
})
return reply.code(201).send({
message: '회원가입이 완료되었습니다.',
@@ -387,6 +392,10 @@ export async function registerAuthRoutes(app) {
}
const verification = await createEmailVerificationToken(user.id)
await sendVerificationEmail({
to: user.email,
linkUrl: verification.previewUrl,
})
return {
message: '이메일 인증 링크를 준비했습니다.',
@@ -471,6 +480,10 @@ export async function registerAuthRoutes(app) {
}
const reset = await createPasswordResetToken(user.id)
await sendPasswordResetEmail({
to: user.email,
linkUrl: reset.previewUrl,
})
return {
message: '비밀번호 재설정 링크를 준비했습니다.',