10000 convert user_metadata to customers table and then change db schema to… · devtodollars/mvp-boilerplate@b7d24c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
convert user_metadata to customers table and then change db schema to…
Browse files Browse the repository at this point in the history
… support subscriptions
  • Loading branch information
matthewwong525 committed Mar 1, 2024
1 parent a2fc6c3 commit b7d24c4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The Supabase Flutter Template is designed to accelerate your app development wit

## Backend - Supabase

- **Authentication**: Pre-configured setup with `user_metadata` for a seamless sign-in/sign-up experience.
- **Authentication**: Pre-configured setup for a seamless sign-in/sign-up experience.
- **Local Development**: Fully configured environment for local development from day one.

## Analytics - Posthog
Expand Down
6 changes: 4 additions & 2 deletions flutter/lib/models/app_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import 'package:supabase_flutter/supabase_flutter.dart';
class AppUser {
Session session;
AuthChangeEvent? authEvent;
String? paymentTier;
List<String> oneTimePaymentProducts;
String? activeSubscriptionProduct;

AppUser({
required this.session,
this.authEvent,
this.paymentTier,
this.oneTimePaymentProducts = const [],
this.activeSubscriptionProduct,
});
}
5 changes: 3 additions & 2 deletions flutter/lib/services/auth_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ class Auth extends _$Auth {
if (session == null) return authStateController.add(null);

final metadata = await client
.from("user_metadata")
.from("customers")
.select()
.eq("user_id", session.user.id)
.maybeSingle();
final user = AppUser(
session: session,
authEvent: state.event,
paymentTier: metadata?["tier"],
oneTimePaymentProducts: metadata?["one_time_payment_products"],
activeSubscriptionProduct: metadata?["active_subscription_product"],
);
authStateController.add(user);
}
Expand Down
25 changes: 15 additions & 10 deletions supabase/functions/get_stripe_url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { stripe } from "../_shared/stripe.ts";

clientRequestHandler(async (req, user) => {
const { price, return_url } = await req.json();
// get stripe information from user_metadata table!
const { data: metadata } = await supabase.from("user_metadata").select(
"stripe_customer_id,tier",
).eq(
// get stripe information from customers table!
const { data } = await supabase.from("customers").select().eq(
"user_id",
user.id,
).maybeSingle();
let stripeCustomerId = metadata?.stripe_customer_id;
const tier = metadata?.tier;
let stripeCustomerId = data?.stripe_customer_id;
const paidProducts: string[] = data?.one_time_payment_products;
const activeSubscriptionProduct: string = data?.active_subscription_product;
if (!stripeCustomerId) {
// create stripe customer if doesn't exist
console.log("create stripe customer");
Expand All @@ -24,14 +23,20 @@ clientRequestHandler(async (req, user) => {
},
});
stripeCustomerId = customer.id;
await supabase.from("user_metadata").upsert({
await supabase.from("customers").upsert({
user_id: user.id,
stripe_customer_id: customer.id,
});
}

// query price product based on price
const priceObj = await stripe.prices.retrieve(price);

// get price based on product
let redirect_url: string | undefined;
if (tier) {
// open billing portal

if ([...paidProducts, activeSubscriptionProduct].includes(priceObj.product)) {
// open billing portal if product has been purchased
console.log("open billing portal session");
const session = await stripe.billingPortal.sessions.create({
customer: stripeCustomerId,
Expand All @@ -43,7 +48,7 @@ clientRequestHandler(async (req, user) => {
console.log("open checkout session");
const session = await stripe.checkout.sessions.create({
customer: stripeCustomerId,
mode: "payment",
mode: (priceObj.type == "recurring") ? "subscription" : "payment",
line_items: [{
price,
quantity: 1,
Expand Down

0 comments on commit b7d24c4

Please sign in to comment.
0