Skip to content

Commit d27188a

Browse files
committed
fixed permission issues
1 parent 2f59ee5 commit d27188a

File tree

3 files changed

+63
-87
lines changed

3 files changed

+63
-87
lines changed

migrations/fix_crypto_payments_permissions.sql

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
-- SIMPLIFIED PERMISSIONS FIX WITHOUT ESCAPING ISSUES
2+
3+
-- First, grant basic permissions directly to tables
4+
GRANT ALL ON TABLE public.stripe_payments TO postgres;
5+
GRANT ALL ON TABLE public.stripe_payments TO service_role;
6+
GRANT SELECT, INSERT, UPDATE ON TABLE public.stripe_payments TO authenticated;
7+
GRANT SELECT, INSERT, UPDATE ON TABLE public.stripe_payments TO anon;
8+
9+
GRANT ALL ON TABLE public.crypto_payments TO postgres;
10+
GRANT ALL ON TABLE public.crypto_payments TO service_role;
11+
GRANT SELECT, INSERT, UPDATE ON TABLE public.crypto_payments TO authenticated;
12+
GRANT SELECT, INSERT, UPDATE ON TABLE public.crypto_payments TO anon;
13+
14+
-- Enable Row Level Security with permissive policies
15+
ALTER TABLE public.stripe_payments ENABLE ROW LEVEL SECURITY;
16+
ALTER TABLE public.crypto_payments ENABLE ROW LEVEL SECURITY;
17+
18+
-- Drop existing policies if they exist
19+
DROP POLICY IF EXISTS stripe_payments_policy ON public.stripe_payments;
20+
DROP POLICY IF EXISTS crypto_payments_policy ON public.crypto_payments;
21+
22+
-- Create new policies
23+
CREATE POLICY stripe_payments_policy ON public.stripe_payments USING (true) WITH CHECK (true);
24+
CREATE POLICY crypto_payments_policy ON public.crypto_payments USING (true) WITH CHECK (true);
25+
26+
-- Grant schema usage
27+
GRANT USAGE ON SCHEMA public TO public;
28+
GRANT USAGE ON SCHEMA public TO anon;
29+
GRANT USAGE ON SCHEMA public TO authenticated;
30+
GRANT USAGE ON SCHEMA public TO service_role;
31+
32+
-- Grant permissions on sequences if they exist
33+
DO $$
34+
BEGIN
35+
-- Grant permissions for all sequences in public schema
36+
EXECUTE (
37+
SELECT 'GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO authenticated;'
38+
);
39+
40+
EXECUTE (
41+
SELECT 'GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO anon;'
42+
);
43+
44+
EXECUTE (
45+
SELECT 'GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO service_role;'
46+
);
47+
END
48+
$$;

src/services/payment-service.js

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -110,56 +110,21 @@ export const paymentService = {
110110
subscription = subData;
111111

112112
// Next, create the crypto_payments record
113-
// Wrap in try/catch to handle permission errors gracefully
114-
let paymentData = null;
115-
let paymentError = null;
116-
117-
try {
118-
const result = await supabase
119-
.from('crypto_payments')
120-
.insert([{
121-
subscription_id: subscription.id,
122-
amount: cryptoAmount,
123-
currency: coin,
124-
exchange_rate_usd: amount / cryptoAmount,
125-
status: 'pending',
126-
payment_data: {
127-
fiat_amount: amount,
128-
fiat_currency: 'USD'
129-
}
130-
}])
131-
.select()
132-
.single();
133-
134-
paymentData = result.data;
135-
paymentError = result.error;
136-
137-
if (paymentError && paymentError.code === '42501') { // Permission denied
138-
console.warn('Permission denied for crypto_payments table, using localStorage fallback instead');
139-
140-
// Create a temporary payment object with a generated ID
141-
// This will be stored in localStorage on the client until permissions are fixed
142-
paymentData = {
143-
id: crypto.randomUUID(),
144-
subscription_id: subscription.id,
145-
amount: cryptoAmount,
146-
currency: coin,
147-
exchange_rate_usd: amount / cryptoAmount,
148-
status: 'pending',
149-
payment_data: {
150-
fiat_amount: amount,
151-
fiat_currency: 'USD'
152-
},
153-
created_at: new Date().toISOString()
154-
};
155-
156-
// Reset the error since we're handling it with our fallback
157-
paymentError = null;
158-
}
159-
} catch (err) {
160-
console.error('Error creating crypto payment record:', err);
161-
paymentError = err;
162-
}
113+
const { data: paymentData, error: paymentError } = await supabase
114+
.from('crypto_payments')
115+
.insert([{
116+
subscription_id: subscription.id,
117+
amount: cryptoAmount,
118+
currency: coin,
119+
exchange_rate_usd: amount / cryptoAmount,
120+
status: 'pending',
121+
payment_data: {
122+
fiat_amount: amount,
123+
fiat_currency: 'USD'
124+
}
125+
}])
126+
.select()
127+
.single();
163128

164129
if (paymentError) throw paymentError;
165130
cryptoPayment = paymentData;

0 commit comments

Comments
 (0)