Skip to content

Commit 6b50522

Browse files
committed
Fix RLS permission issues
1 parent fbcf5aa commit 6b50522

File tree

6 files changed

+31
-15
lines changed

6 files changed

+31
-15
lines changed

app/core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class OperationCost(TypedDict):
88

99
class Settings(BaseSettings):
1010
PROJECT_NAME: str
11+
PROJECT_URL: str
1112

1213
# List all published versions here. This enables us to manage them better; i.e. gradually phase them out etc...
1314
API_V1_STR: str

app/core/security.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ async def authenticate_user(email: str, password: str):
1919
if not user.user_metadata["email_verified"]:
2020
raise HTTPException(status_code=401, detail="Email not verified. Please check your inpox & spam")
2121

22+
print("User authenticated:", user)
2223
# Check if user has credits entry
2324
credits = db.table('credits').select('*').eq('user_id', user.id).execute()
25+
print("User credits:", credits.data)
2426
if not credits.data:
2527
# Create initial credits entry if none exists
26-
db.table('credits').insert({
27-
'user_id': user.id,
28-
'balance': settings.USER_DAILY_CREDITS # starting balance for registered users
29-
}).execute()
30-
28+
db.rpc('add_credits', {
29+
'p_user_id': user.id,
30+
'amount': settings.USER_DAILY_CREDITS
31+
}).execute()
3132
return user
3233

3334
async def verify_email_code(email: str, code: str):

supabase/config.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ max_client_conn = 100
4444

4545
[db.seed]
4646
# If enabled, seeds the database after migrations during a db reset.
47-
enabled = true
47+
enabled = false
4848
# Specifies an ordered list of seed files to load during db reset.
4949
# Supports glob patterns relative to supabase directory: './seeds/*.sql'
5050
sql_paths = ['./seed.sql']
@@ -97,7 +97,7 @@ file_size_limit = "50MiB"
9797
enabled = true
9898
# The base URL of your website. Used as an allow-list for redirects and for constructing URLs used
9999
# in emails.
100-
site_url = "http://127.0.0.1:3000"
100+
site_url = "env(PROJECT_URL)"
101101
# A list of *exact* URLs that auth providers are permitted to redirect to post authentication.
102102
additional_redirect_urls = ["https://127.0.0.1:3000"]
103103
# How long tokens are valid for, in seconds. Defaults to 3600 (1 hour), maximum 604,800 (1 week).
@@ -110,7 +110,7 @@ refresh_token_reuse_interval = 10
110110
# Allow/disallow new user signups to your project.
111111
enable_signup = true
112112
# Allow/disallow anonymous sign-ins to your project.
113-
enable_anonymous_sign_ins = false
113+
enable_anonymous_sign_ins = true
114114
# Allow/disallow testing manual linking of accounts
115115
enable_manual_linking = false
116116
# Passwords shorter than this value will be rejected as weak. Minimum 6, recommended 8 or more.

supabase/migrations/001_credit_system.sql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ create table credits (
99
last_free_credit_update timestamp with time zone,
1010
primary key (user_id)
1111
);
12+
ALTER TABLE public.credits ENABLE ROW LEVEL SECURITY;
1213

1314
-- Credit transactions table
1415
create table credit_transactions (
@@ -18,6 +19,7 @@ create table credit_transactions (
1819
operation_type text not null,
1920
created_at timestamp with time zone default now()
2021
);
22+
ALTER TABLE public.credit_transactions ENABLE ROW LEVEL SECURITY;
2123

2224
-- RLS Policies
2325
create policy "Users can view their own credits"
@@ -30,13 +32,13 @@ create policy "System can modify credits"
3032

3133
-- Function to add credits
3234
create or replace function add_credits(
33-
user_id uuid,
35+
p_user_id uuid,
3436
amount integer
3537
) returns void
3638
language plpgsql security definer as $$
3739
begin
3840
insert into credits (user_id, balance)
39-
values (add_credits.user_id, amount)
41+
values (p_user_id, amount)
4042
on conflict (user_id) do update
4143
set balance = credits.balance + amount;
4244

@@ -45,7 +47,7 @@ begin
4547
amount,
4648
operation_type
4749
) values (
48-
add_credits.user_id,
50+
p_user_id,
4951
amount,
5052
'addition'
5153
);
@@ -87,4 +89,5 @@ begin
8789

8890
return false;
8991
end;
90-
$$;
92+
$$;
93+

supabase/migrations/002_api_keys.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,17 @@ create table api_keys (
44
created_at timestamp with time zone default now(),
55
revoked_at timestamp with time zone
66
);
7+
ALTER TABLE public.api_keys ENABLE ROW LEVEL SECURITY;
8+
9+
-- Add after table creation
10+
create policy "Users can view their own API keys"
11+
on api_keys for select
12+
using (auth.uid() = user_id);
13+
14+
create policy "Users can create their own API keys"
15+
on api_keys for insert
16+
with check (auth.uid() = user_id);
17+
18+
create policy "Users can delete their own API keys"
19+
on api_keys for delete
20+
using (auth.uid() = user_id);

supabase/migrations/003_rls_updates.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)