Skip to content

Commit 655b5b4

Browse files
authored
Merge pull request #822 from CodingCatDev/dev
release: send portal sign-in codes via Cloudflare Email Sending
2 parents cf49a08 + 0adf1cf commit 655b5b4

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎apps/site/src/lib/auth.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { env } from "cloudflare:workers";
22
import { mcp } from "@better-auth/mcp";
33
import { betterAuth } from "better-auth";
44
import { emailOTP, jwt, magicLink } from "better-auth/plugins";
5-
import { sendCodingCatEmail } from "@/lib/email";
5+
import { sendCodingCatAuthEmail } from "@/lib/email";
66

77
export const BETTER_AUTH_SECRET =
88
(env as unknown as { BETTER_AUTH_SECRET?: string })?.BETTER_AUTH_SECRET ||
@@ -211,7 +211,7 @@ function buildPortalAuthPlugins() {
211211
</div>
212212
`;
213213

214-
await sendCodingCatEmail({
214+
await sendCodingCatAuthEmail({
215215
to: email,
216216
subject,
217217
text,
@@ -260,7 +260,7 @@ function buildPortalAuthPlugins() {
260260
</div>
261261
`;
262262

263-
await sendCodingCatEmail({
263+
await sendCodingCatAuthEmail({
264264
to: email,
265265
subject,
266266
text,

‎apps/site/src/lib/email.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,37 @@ export async function sendCodingCatEmailDetailed(
424424
};
425425
}
426426

427+
/**
428+
* Sign-in codes and magic links are the one exception to the disabled
429+
* automated sending above: nobody can sign in without them. They go through
430+
* Cloudflare Email Sending, which requires `from` on the onboarded
431+
* codingcat.dev domain.
432+
*/
433+
export async function sendCodingCatAuthEmail(opts: {
434+
to: string;
435+
subject: string;
436+
text: string;
437+
html: string;
438+
}): Promise<void> {
439+
const workerEnv = (env ?? {}) as unknown as { EMAIL?: SendEmailBinding };
440+
if (!workerEnv.EMAIL) {
441+
throw new Error("EMAIL binding is not configured");
442+
}
443+
try {
444+
await workerEnv.EMAIL.send({
445+
to: opts.to,
446+
from: { email: "noreply@codingcat.dev", name: "CodingCat.dev" },
447+
subject: opts.subject,
448+
text: opts.text,
449+
html: opts.html,
450+
});
451+
} catch (error) {
452+
const code = (error as { code?: string })?.code ?? "unknown";
453+
console.error(`Sign-in email failed: ${code}`);
454+
throw error;
455+
}
456+
}
457+
427458
export async function sendCodingCatEmail(
428459
opts: CodingCatEmailOptions,
429460
): Promise<boolean> {

0 commit comments

Comments
 (0)