🔐 User Authentication & Payments
Thêm authentication và payment system vào AI apps.
Why Auth & Payments?
Text
1Benefits:2- Personalized experience3- Usage tracking per user4- Monetization5- Data privacy6- Feature gatingUser Authentication
Bubble Built-in Auth
Text
1User Data Type (built-in):2- email3- password (encrypted)4- created date5- modified date6 7Add custom fields:8- plan_type (text)9- usage_count (number)10- usage_reset_date (date)11- profile_picture (image)Signup Flow
Text
1Signup Page Elements:21. Email input32. Password input43. Confirm password input54. Signup button65. "Already have account?" linkSignup Workflow
Text
1Button "Sign Up" clicked:2 31. Validate4 - Email format valid5 - Password min 8 chars6 - Passwords match7 82. Sign user up9 - Email: Email Input's value10 - Password: Password Input's value11 123. Initialize user13 - Set plan_type = "free"14 - Set usage_count = 015 - Set usage_reset_date = Current date16 174. Navigate to appLogin Flow
Text
1Login Page Elements:21. Email input32. Password input43. Login button54. "Forgot password?" link65. "Sign up" linkLogin Workflow
Text
1Button "Login" clicked:2 31. Log user in4 - Email: Email Input5 - Password: Password Input6 72. If login successful:8 - Navigate to app9 103. If login fails:11 - Show error messageOAuth (Google, etc.)
Text
1In Bubble:21. Plugins → Add Google OAuth32. Configure in Google Console43. Add "Login with Google" button54. Workflow: Login with GoogleMembership Tiers
Plan Structure
Text
1Free Plan:2- 10 AI requests/day3- Basic features4- No history export5 6Pro Plan ($9/month):7- 100 AI requests/day8- All features9- Export & history10- Priority support11 12Business Plan ($29/month):13- Unlimited requests14- API access15- Team features16- Custom brandingDatabase Updates
Text
1User fields:2- plan_type: free/pro/business3- subscription_id: Stripe subscription ID4- subscription_status: active/cancelled/past_due5- usage_count: number (daily)6- usage_reset_date: dateUsage Tracking
Text
1Check before AI call:2 31. Get user's plan_type42. Get usage_count53. Get limit for plan64. If usage_count >= limit7 - Show upgrade prompt8 - Stop workflow95. Else10 - Continue11 - Increment usage_countReset Daily Usage
Text
1Scheduled workflow (runs daily):2 3Search for Users:4 where usage_reset_date < Current date - 1 day5 6Make changes to list:7 - usage_count = 08 - usage_reset_date = Current dateFeature Gating
Conditional Display
Text
1Show Pro features only to Pro users:2 3Element conditional:4- Visible when: Current User's plan_type is "pro"5 6Or hide with blur + upgrade promptWorkflow Gates
Text
1Before premium action:2 3Only when: Current User's plan_type is "pro"4 5Else: Show upgrade popupPayment Integration
Stripe Setup
Stripe Integration
Text
1Steps:21. Create Stripe account32. Get API keys (test mode first)43. Install Stripe plugin in Bubble54. Configure API keys65. Create products in Stripe DashboardStripe Products
Text
1In Stripe Dashboard:2 3Product 1: Pro Monthly4- Price: $9/month5- Recurring6 7Product 2: Business Monthly8- Price: $29/month9- Recurring10 11Get Price IDs for eachCheckout Flow
Text
1Button "Upgrade to Pro" clicked:2 31. Create Stripe Checkout Session4 - Price ID: pro_monthly_price_id5 - Success URL: your-app.com/success6 - Cancel URL: your-app.com/pricing7 - Customer email: Current User's email8 92. Navigate to Stripe Checkout URLSuccess Page
Text
1On success page load:2 31. Get session_id from URL42. Retrieve Stripe session details53. Update User:6 - plan_type = "pro"7 - subscription_id = session's subscription ID8 - subscription_status = "active"94. Show success message105. Navigate to appWebhook Handling
Why Webhooks?
Text
1Stripe events to handle:2- Payment successful3- Subscription renewed4- Payment failed5- Subscription cancelledSetup Webhook
Text
11. In Stripe Dashboard → Webhooks22. Add endpoint: your-app.com/api/stripe-webhook33. Select events to listen4 54. In Bubble → Backend Workflows65. Create API workflow: stripe-webhook76. Handle each event typeHandle Events
Text
1Event: customer.subscription.deleted2 31. Get subscription_id from event42. Search User where subscription_id = event's ID53. Update User:6 - plan_type = "free"7 - subscription_status = "cancelled"Pricing Page
Layout
Text
1┌────────────────────────────────────────────────┐2│ Choose Your Plan │3├──────────────┬───────────────┬────────────────┤4│ FREE │ PRO │ BUSINESS │5│ $0 │ $9/mo │ $29/mo │6├──────────────┼───────────────┼────────────────┤7│ 10 req/day │ 100 req/day │ Unlimited │8│ Basic AI │ All AI tools │ All AI tools │9│ No export │ Export │ Export + API │10│ - │ History │ Team access │11│ - │ Support │ Priority │12├──────────────┼───────────────┼────────────────┤13│ [Current] │ [Upgrade] │ [Upgrade] │14└──────────────┴───────────────┴────────────────┘Dynamic Buttons
Text
1Conditionals:2 3If user's plan = this plan:4- Text: "Current Plan"5- Disabled6 7If user's plan < this plan:8- Text: "Upgrade"9- Clickable10 11If user's plan > this plan:12- Hidden or "Contact Us"Cancel Subscription
Cancel Flow
Text
1Button "Cancel Subscription" clicked:2 31. Confirm popup4 "Are you sure? You'll lose Pro features."5 62. Call Stripe API: Cancel Subscription7 - Subscription ID: Current User's subscription_id8 - Cancel at period end: yes (grace period)9 103. Update User:11 - subscription_status = "cancelling"12 134. Show confirmationFree Trial
Trial Setup
Text
1Stripe subscription with trial:2- Trial period: 7 days3- No charge during trial4- Auto-charges after trial5 6User fields:7- trial_end_date8- has_used_trial (yes/no)Trial Workflow
Text
1New user signup:2 31. Check if eligible for trial4 - has_used_trial = no5 62. Create subscription with trial7 - trial_period_days: 78 93. Update User:10 - has_used_trial = yes11 - trial_end_date = +7 daysUsage Dashboard
User Dashboard
Text
1┌────────────────────────────────┐2│ Your Plan: Pro │3│ Usage: 45/100 today │4│ ████████░░░░░ 45% │5│ │6│ Renews: Jan 15, 2025 │7│ [Manage Subscription] │8└────────────────────────────────┘Implementation
Text
1Progress bar width:2(Current User's usage_count / plan_limit) * 100 %3 4Text display:5Current User's usage_count / plan_limit " today"Security Best Practices
Security
Text
11. Server-side validation2 - Don't trust client-side only3 - Verify plan in backend4 52. Secure webhooks6 - Verify Stripe signature7 - Use webhook secrets8 93. Protect sensitive data10 - Don't expose subscription IDs11 - Privacy rules on database12 134. Handle edge cases14 - Payment failures15 - Expired cards16 - Cancelled but active periodTesting
Test Mode
Text
1Use Stripe test mode:2- Test card: 4242 4242 4242 42423- Any future expiry4- Any CVC5 6Test scenarios:7- Successful payment8- Failed payment (use 4000000000000002)9- Cancel subscription10- Webhook eventsBài Tập
Practice
Implement Auth & Payments:
- Set up user signup/login
- Create membership tiers
- Implement usage tracking
- Set up Stripe (test mode)
- Build pricing page
- Create checkout flow
- Handle webhooks
- Test full flow
Tiếp theo: Bài 13 - Deployment & Hosting
