1010// SUPABASE_ANON_KEY — (auto-set by Supabase)
1111// SUPABASE_SERVICE_ROLE_KEY — (auto-set by Supabase)
1212
13- import { createClient } from "https://esm.sh/@supabase/supabase-js@2" ;
1413import { corsHeaders } from "../_shared/cors.ts" ;
1514import { stripe } from "../_shared/stripe.ts" ;
1615import { supabaseAdmin } from "../_shared/supabase-admin.ts" ;
16+ import { authenticateUser } from "../_shared/auth.ts" ;
1717
1818Deno . serve ( async ( req ) => {
19- // Handle CORS preflight
2019 if ( req . method === "OPTIONS" ) {
2120 return new Response ( "ok" , { headers : corsHeaders } ) ;
2221 }
2322
2423 try {
25- // Authenticate the user from the Authorization header
26- const authHeader = req . headers . get ( "Authorization" ) ! ;
27- const supabase = createClient ( Deno . env . get ( "SUPABASE_URL" ) ! , Deno . env . get ( "SUPABASE_ANON_KEY" ) ! , {
28- global : { headers : { Authorization : authHeader } } ,
29- } ) ;
30-
31- const {
32- data : { user } ,
33- error : authError ,
34- } = await supabase . auth . getUser ( ) ;
35- if ( authError || ! user ) {
36- return new Response ( JSON . stringify ( { error : "Unauthorized" } ) , {
37- status : 401 ,
38- headers : { ...corsHeaders , "Content-Type" : "application/json" } ,
39- } ) ;
40- }
24+ const auth = await authenticateUser ( req ) ;
25+ if ( "response" in auth ) return auth . response ;
26+ const { user } = auth ;
4127
4228 // Check if user already has a Stripe customer ID
4329 const { data : subscription } = await supabaseAdmin
@@ -56,7 +42,6 @@ Deno.serve(async (req) => {
5642 } ) ;
5743 customerId = customer . id ;
5844
59- // Store the customer ID
6045 await supabaseAdmin . from ( "subscriptions" ) . upsert (
6146 {
6247 user_id : user . id ,
@@ -67,20 +52,13 @@ Deno.serve(async (req) => {
6752 ) ;
6853 }
6954
70- // Parse optional return URL from request body
7155 const body = await req . json ( ) . catch ( ( ) => ( { } ) ) ;
7256 const returnUrl = body . returnUrl || Deno . env . get ( "SITE_URL" ) || "http://localhost:5173" ;
7357
74- // Create Checkout Session
7558 const session = await stripe . checkout . sessions . create ( {
7659 customer : customerId ,
7760 mode : "subscription" ,
78- line_items : [
79- {
80- price : Deno . env . get ( "STRIPE_PRICE_ID" ) ! ,
81- quantity : 1 ,
82- } ,
83- ] ,
61+ line_items : [ { price : Deno . env . get ( "STRIPE_PRICE_ID" ) ! , quantity : 1 } ] ,
8462 success_url : `${ returnUrl } ?checkout=success` ,
8563 cancel_url : `${ returnUrl } ?checkout=cancelled` ,
8664 } ) ;
@@ -89,8 +67,9 @@ Deno.serve(async (req) => {
8967 headers : { ...corsHeaders , "Content-Type" : "application/json" } ,
9068 } ) ;
9169 } catch ( err ) {
70+ const message = err instanceof Error ? err . message : "Internal server error" ;
9271 console . error ( "create-checkout error:" , err ) ;
93- return new Response ( JSON . stringify ( { error : err . message } ) , {
72+ return new Response ( JSON . stringify ( { error : message } ) , {
9473 status : 500 ,
9574 headers : { ...corsHeaders , "Content-Type" : "application/json" } ,
9675 } ) ;
0 commit comments