-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers_table.sql
More file actions
49 lines (42 loc) · 1.49 KB
/
users_table.sql
File metadata and controls
49 lines (42 loc) · 1.49 KB
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
46
47
48
49
-- Create users table if it doesn't exist
CREATE TABLE IF NOT EXISTS public.users (
id UUID PRIMARY KEY,
email TEXT NOT NULL,
first_name TEXT,
last_name TEXT,
phone TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);
-- Set up Row Level Security (RLS) policies
ALTER TABLE public.users ENABLE ROW LEVEL SECURITY;
-- Create policies
-- Allow users to read their own data
CREATE POLICY "Users can view own data" ON public.users
FOR SELECT USING (auth.uid() = id);
-- Allow users to insert their own data
CREATE POLICY "Users can insert own data" ON public.users
FOR INSERT WITH CHECK (auth.uid() = id);
-- Allow users to update their own data
CREATE POLICY "Users can update own data" ON public.users
FOR UPDATE USING (auth.uid() = id);
-- Function to handle user signups
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.users (id, email, created_at, updated_at)
VALUES (new.id, new.email, now(), now())
ON CONFLICT (id) DO NOTHING;
RETURN new;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Trigger for user signups
DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
-- Grant necessary privileges
GRANT ALL ON public.users TO postgres;
GRANT ALL ON public.users TO anon;
GRANT ALL ON public.users TO authenticated;
GRANT ALL ON public.users TO service_role;