File size: 1,535 Bytes
836f140
b2a340a
56d9508
b2a340a
 
 
 
 
836f140
 
5fb463b
836f140
56d9508
836f140
 
5fb463b
 
836f140
 
 
 
5fb463b
836f140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- Users table
CREATE TABLE IF NOT EXISTS public.users (
    user_id varchar(16) PRIMARY KEY, -- Use BIGINT for larger integer values
    username TEXT UNIQUE NOT NULL,
    password TEXT NOT NULL,
    email TEXT,
    date_joined TIMESTAMP WITH TIME ZONE NOT NULL,
    access_level TEXT NOT NULL
);

-- Sessions table with composite primary key (user_id, token)
create table if not exists public.sessions (
    user_id varchar(16) references public.users(user_id),
    token text not null,
    expires timestamp with time zone not null,
    device text not null,
    primary key (user_id, token)  -- Composite primary key
);

-- Create indexes for better performance
create index if not exists idx_users_username on public.users(username);
create index if not exists idx_users_user_id on public.users(user_id);
create index if not exists idx_sessions_user_id on public.sessions(user_id);
create index if not exists idx_sessions_token on public.sessions(token);

-- Set up Row Level Security (RLS)
alter table public.users enable row level security;
alter table public.sessions enable row level security;

-- Create policies
create policy "Enable read access for all users"
    on public.users for select
    using (true);

create policy "Enable insert for authenticated users only"
    on public.users for insert
    with check (true);

create policy "Enable update for authenticated users"
    on public.users for update
    using (true);

create policy "Enable all access for sessions"
    on public.sessions for all
    using (true);