1- import { components } from "../_generated/api" ;
1+ import { components , api } from "../_generated/api" ;
22import authSchema from "../betterAuth/schema" ;
33import { createClient , GenericCtx } from "@convex-dev/better-auth" ;
44import { betterAuth , type BetterAuthOptions } from "better-auth/minimal" ;
@@ -11,12 +11,6 @@ import {
1111 magicLink ,
1212 emailOTP ,
1313} from "better-auth/plugins" ;
14- import {
15- sendMagicLink ,
16- sendOTPVerification ,
17- sendEmailVerification ,
18- sendResetPassword ,
19- } from "../emails/email" ;
2014import { requireActionCtx } from "@convex-dev/better-auth/utils" ;
2115import { DataModel } from "../_generated/dataModel" ;
2216import authConfig from "../auth.config" ;
@@ -29,13 +23,18 @@ export const authComponent = createClient<DataModel, typeof authSchema>(
2923 local : {
3024 schema : authSchema ,
3125 } ,
32- verbose : false ,
26+ verbose : true ,
3327 } ,
3428) ;
3529
3630export const createAuthOptions = ( ctx : GenericCtx < DataModel > ) => {
31+ const finalSiteUrl = ( process . env . SITE_URL || process . env . BETTER_AUTH_URL || "https://localhost:3000" ) as string ;
32+ if ( ! process . env . SITE_URL && ! process . env . BETTER_AUTH_URL ) {
33+ console . warn ( "WARNING: SITE_URL or BETTER_AUTH_URL is not set in Convex environment variables. This will cause INVALID_ORIGIN errors." ) ;
34+ }
3735 return {
38- baseURL : siteUrl ,
36+ baseURL : finalSiteUrl ,
37+ trustedOrigins : [ finalSiteUrl ] ,
3938 database : authComponent . adapter ( ctx ) ,
4039 account : {
4140 accountLinking : {
@@ -45,19 +44,24 @@ export const createAuthOptions = (ctx: GenericCtx<DataModel>) => {
4544 } ,
4645 emailVerification : {
4746 sendVerificationEmail : async ( { user, url } ) => {
48- await sendEmailVerification ( requireActionCtx ( ctx ) , {
47+ // Ensure the URL uses the correct protocol and path
48+ const verificationUrl = url . replace ( "http://localhost:3000" , finalSiteUrl )
49+ . replace ( "/api/auth/verify-email" , "/verify-email" ) ;
50+
51+ await requireActionCtx ( ctx ) . runAction ( api . emails . email . sendEmailVerification , {
4952 to : user . email ,
50- url,
53+ url : verificationUrl ,
5154 } ) ;
5255 } ,
5356 } ,
5457 emailAndPassword : {
5558 enabled : true ,
5659 requireEmailVerification : true ,
5760 sendResetPassword : async ( { user, url } ) => {
58- await sendResetPassword ( requireActionCtx ( ctx ) , {
61+ const resetPasswordUrl = url . replace ( "http://localhost:3000" , finalSiteUrl ) ;
62+ await requireActionCtx ( ctx ) . runAction ( api . emails . email . sendResetPassword , {
5963 to : user . email ,
60- url,
64+ url : resetPasswordUrl ,
6165 } ) ;
6266 } ,
6367 } ,
@@ -89,15 +93,15 @@ export const createAuthOptions = (ctx: GenericCtx<DataModel>) => {
8993 username ( ) ,
9094 magicLink ( {
9195 sendMagicLink : async ( { email, url } ) => {
92- await sendMagicLink ( requireActionCtx ( ctx ) , {
96+ await requireActionCtx ( ctx ) . runAction ( api . emails . email . sendMagicLink , {
9397 to : email ,
9498 url,
9599 } ) ;
96100 } ,
97101 } ) ,
98102 emailOTP ( {
99103 async sendVerificationOTP ( { email, otp } ) {
100- await sendOTPVerification ( requireActionCtx ( ctx ) , {
104+ await requireActionCtx ( ctx ) . runAction ( api . emails . email . sendOTPVerification , {
101105 to : email ,
102106 code : otp ,
103107 } ) ;
0 commit comments