-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_auth_migration.sql
More file actions
146 lines (120 loc) · 5.85 KB
/
Copy pathsupabase_auth_migration.sql
File metadata and controls
146 lines (120 loc) · 5.85 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- ==============================================================================
-- Supabase Native Authentication Migration & RLS Security Policies
-- ==============================================================================
-- Run this script in your Supabase SQL Editor (Dashboard > SQL Editor) to ensure
-- Row Level Security (RLS) policies work seamlessly with native Supabase Auth.
-- ==============================================================================
-- 1. Create or update user_profiles table linked to Supabase auth.users
CREATE TABLE IF NOT EXISTS public.user_profiles (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
email TEXT,
full_name TEXT,
avatar_url TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Enable RLS on user_profiles
ALTER TABLE public.user_profiles ENABLE ROW LEVEL SECURITY;
-- Drop old conflicting policies if they exist
DROP POLICY IF EXISTS "Users can read their own profile" ON public.user_profiles;
DROP POLICY IF EXISTS "Users can update their own profile" ON public.user_profiles;
DROP POLICY IF EXISTS "Public profiles are viewable by everyone" ON public.user_profiles;
CREATE POLICY "Users can read their own profile"
ON public.user_profiles
FOR SELECT
USING (auth.uid() = id);
CREATE POLICY "Users can update their own profile"
ON public.user_profiles
FOR UPDATE
USING (auth.uid() = id);
CREATE POLICY "Users can insert their own profile"
ON public.user_profiles
FOR INSERT
WITH CHECK (auth.uid() = id);
-- 2. Automatically sync new users from auth.users to public.user_profiles
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.user_profiles (id, email, full_name, avatar_url)
VALUES (
NEW.id,
NEW.email,
COALESCE(NEW.raw_user_meta_data->>'full_name', NEW.raw_user_meta_data->>'name', split_part(NEW.email, '@', 1)),
COALESCE(NEW.raw_user_meta_data->>'avatar_url', NEW.raw_user_meta_data->>'picture', NULL)
)
ON CONFLICT (id) DO UPDATE SET
email = EXCLUDED.email,
full_name = COALESCE(EXCLUDED.full_name, user_profiles.full_name),
avatar_url = COALESCE(EXCLUDED.avatar_url, user_profiles.avatar_url),
updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
CREATE TRIGGER on_auth_user_created
AFTER INSERT OR UPDATE ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
-- 3. Update Subscriptions RLS Policies for Supabase Auth (auth.uid())
ALTER TABLE IF EXISTS public.subscriptions ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "Users can view their own subscription" ON public.subscriptions;
DROP POLICY IF EXISTS "Users can update their own subscription" ON public.subscriptions;
DROP POLICY IF EXISTS "Users can insert their own subscription" ON public.subscriptions;
CREATE POLICY "Users can view their own subscription"
ON public.subscriptions
FOR SELECT
USING (auth.uid()::text = user_id OR auth.uid()::text = user_id::text);
CREATE POLICY "Users can insert their own subscription"
ON public.subscriptions
FOR INSERT
WITH CHECK (auth.uid()::text = user_id OR auth.uid()::text = user_id::text);
CREATE POLICY "Users can update their own subscription"
ON public.subscriptions
FOR UPDATE
USING (auth.uid()::text = user_id OR auth.uid()::text = user_id::text);
-- 4. Update Documents RLS Policies for Supabase Auth
ALTER TABLE IF EXISTS public.documents ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "Users can view their own documents" ON public.documents;
DROP POLICY IF EXISTS "Users can create their own documents" ON public.documents;
DROP POLICY IF EXISTS "Users can update their own documents" ON public.documents;
DROP POLICY IF EXISTS "Users can delete their own documents" ON public.documents;
CREATE POLICY "Users can view their own documents"
ON public.documents
FOR SELECT
USING (auth.uid()::text = user_id OR auth.uid()::text = user_id::text);
CREATE POLICY "Users can create their own documents"
ON public.documents
FOR INSERT
WITH CHECK (auth.uid()::text = user_id OR auth.uid()::text = user_id::text);
CREATE POLICY "Users can update their own documents"
ON public.documents
FOR UPDATE
USING (auth.uid()::text = user_id OR auth.uid()::text = user_id::text);
CREATE POLICY "Users can delete their own documents"
ON public.documents
FOR DELETE
USING (auth.uid()::text = user_id OR auth.uid()::text = user_id::text);
-- 5. Update Document Templates RLS Policies
ALTER TABLE IF EXISTS public.document_templates ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "Users can view system and their own templates" ON public.document_templates;
DROP POLICY IF EXISTS "Users can insert their own templates" ON public.document_templates;
DROP POLICY IF EXISTS "Users can update their own templates" ON public.document_templates;
DROP POLICY IF EXISTS "Users can delete their own templates" ON public.document_templates;
CREATE POLICY "Users can view system and their own templates"
ON public.document_templates
FOR SELECT
USING (is_system = true OR created_by = auth.uid()::text);
CREATE POLICY "Users can insert their own templates"
ON public.document_templates
FOR INSERT
WITH CHECK (created_by = auth.uid()::text);
CREATE POLICY "Users can update their own templates"
ON public.document_templates
FOR UPDATE
USING (created_by = auth.uid()::text);
CREATE POLICY "Users can delete their own templates"
ON public.document_templates
FOR DELETE
USING (created_by = auth.uid()::text);
-- ==============================================================================
-- Migration complete! Native Supabase Auth is now configured.
-- ==============================================================================