File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { env } from "cloudflare:workers";
22import { mcp } from "@better-auth/mcp" ;
33import { betterAuth } from "better-auth" ;
44import { emailOTP , jwt , magicLink } from "better-auth/plugins" ;
5- import { sendCodingCatEmail } from "@/lib/email" ;
5+ import { sendCodingCatAuthEmail } from "@/lib/email" ;
66
77export 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,
Original file line number Diff line number Diff 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+
427458export async function sendCodingCatEmail (
428459 opts : CodingCatEmailOptions ,
429460) : Promise < boolean > {
You can’t perform that action at this time.
0 commit comments