@@ -223,25 +223,34 @@ export const auth = betterAuth({
223223 // webhooks have to be publicly accessible
224224 // ngrok http http://localhost:3000
225225 secret : process . env . POLAR_WEBHOOK_SECRET as string ,
226- onSubscriptionUpdated : async ( payload ) => {
227- const authContext = await auth . $context ;
228- const userId = payload . data . customer . externalId ;
229- if ( ! userId ) {
230- return ;
231- }
232- if ( payload . data . status === "active" ) {
233- const productId = payload . data . product . id ;
234- const planType = Object . values ( PRODUCTS ) . find (
235- ( p ) => p . id === productId ,
236- ) ?. slug ;
237- await authContext . internalAdapter . updateUser ( userId , {
238- planType,
239- } ) ;
240- } else {
241- // No active subscription, so we need to remove the plan type
242- await authContext . internalAdapter . updateUser ( userId , {
243- planType : null ,
244- } ) ;
226+ async onPayload ( payload ) {
227+ switch ( payload . type ) {
228+ case "subscription.active" :
229+ case "subscription.canceled" :
230+ case "subscription.updated" :
231+ case "subscription.revoked" :
232+ case "subscription.created" :
233+ case "subscription.uncanceled" : {
234+ const authContext = await auth . $context ;
235+ const userId = payload . data . customer . externalId ;
236+ if ( ! userId ) {
237+ return ;
238+ }
239+ if ( payload . data . status === "active" ) {
240+ const productId = payload . data . product . id ;
241+ const planType = Object . values ( PRODUCTS ) . find (
242+ ( p ) => p . id === productId ,
243+ ) ?. slug ;
244+ await authContext . internalAdapter . updateUser ( userId , {
245+ planType,
246+ } ) ;
247+ } else {
248+ // No active subscription, so we need to remove the plan type
249+ await authContext . internalAdapter . updateUser ( userId , {
250+ planType : null ,
251+ } ) ;
252+ }
253+ }
245254 }
246255 } ,
247256 } ,
@@ -262,6 +271,15 @@ export const auth = betterAuth({
262271 // After verifying email, send them a welcome email
263272 const newSession = ctx . context . newSession ;
264273 if ( newSession ) {
274+ const oneMinuteAgo = new Date ( Date . now ( ) - 60 * 1000 ) ;
275+ if (
276+ ctx . path === "/magic-link/verify" &&
277+ newSession . user . createdAt < oneMinuteAgo
278+ ) {
279+ // magic link is for an account that was created more than a minute ago, so just a normal sign in
280+ // no need to send welcome email
281+ return false ;
282+ }
265283 await sendEmail ( {
266284 to : newSession . user . email ,
267285 template : "welcome" ,
0 commit comments