moved tier free user subscription to createUser event from session to… #18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
… ensure only one subscription per user
Description 📣
i was having this problem where new users would be subscribed 2-3x on Stripe upon logging in and couldn’t figure out why.
The reason why is this block of code on line 23 from
auth.ts
inauthOptions
of the Metered Billing example:The duplicate subscriptions would cause some weird bugs, primarily
tier.report()
reporting to the wrong subscription andtier.limit()
not displaying the correct usage, etc.the correct place for this try/catch block in
authOptions
is thecreateUser
trigger in events:This way, it only fires on the first time the org is not found, and not every time
getSession
runs.Type ✨
Tests 🛠️
Before the code change, 3 subscriptions were created, presumably because
getSession
was fired 3 times in the auth process before the response fromprisma.user.create()
returns, triggering a creation for each session call. Also,tier.report()
would not increment on the client side because the wrong subscription was tied to the user.after clearing Stripe test data and db, subscription is created only once in Stripe and
tier.can()
/tier.report()
works as expected.