@@ -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 } ,
@@ -254,11 +263,7 @@ export const auth = betterAuth({
254263 } ,
255264 hooks : {
256265 after : createAuthMiddleware ( async ( ctx ) => {
257- if (
258- ctx . path === "/magic-link/verify" ||
259- ctx . path === "/verify-email" ||
260- ctx . path === "/sign-in/social"
261- ) {
266+ if ( ctx . path === "/verify-email" || ctx . path === "/sign-in/social" ) {
262267 // After verifying email, send them a welcome email
263268 const newSession = ctx . context . newSession ;
264269 if ( newSession ) {
0 commit comments