-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-users-table.sql
More file actions
130 lines (112 loc) · 3.77 KB
/
fix-users-table.sql
File metadata and controls
130 lines (112 loc) · 3.77 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
-- Enhanced Users Table Setup with Better Error Handling
-- Run this in your Supabase SQL Editor to fix authentication issues
-- First, check if table exists and drop if needed for clean setup
DROP TABLE IF EXISTS public.users CASCADE;
-- Create users table with proper structure
CREATE TABLE public.users (
id UUID REFERENCES auth.users(id) ON DELETE CASCADE PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
full_name TEXT DEFAULT '',
username TEXT UNIQUE,
bio TEXT DEFAULT '',
avatar_url TEXT DEFAULT '',
university TEXT DEFAULT '',
major TEXT DEFAULT '',
year TEXT DEFAULT '',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Enable Row Level Security
ALTER TABLE public.users ENABLE ROW LEVEL SECURITY;
-- Create comprehensive RLS policies
-- Users can view their own profile
CREATE POLICY "users_select_own" ON public.users
FOR SELECT USING (auth.uid() = id);
-- Users can update their own profile
CREATE POLICY "users_update_own" ON public.users
FOR UPDATE USING (auth.uid() = id);
-- Users can insert their own profile
CREATE POLICY "users_insert_own" ON public.users
FOR INSERT WITH CHECK (auth.uid() = id);
-- Allow service role to manage all users (for triggers)
CREATE POLICY "service_role_all_users" ON public.users
FOR ALL USING (current_setting('role') = 'service_role');
-- Create improved trigger function with better error handling
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
DECLARE
user_username TEXT;
user_full_name TEXT;
BEGIN
-- Extract user data with proper fallbacks
user_full_name := COALESCE(NEW.raw_user_meta_data->>'full_name', '');
user_username := COALESCE(
LOWER(REPLACE(NEW.raw_user_meta_data->>'full_name', ' ', '')),
LOWER(REPLACE(COALESCE(NEW.email, ''), '@', '_'))
);
-- Insert user record with comprehensive data
INSERT INTO public.users (
id,
email,
full_name,
username,
created_at,
updated_at
) VALUES (
NEW.id,
NEW.email,
user_full_name,
user_username,
NOW(),
NOW()
);
-- Log successful creation
RAISE NOTICE 'User profile created for: %', NEW.email;
RETURN NEW;
EXCEPTION
WHEN unique_violation THEN
-- Handle duplicate username by appending numbers
user_username := user_username || '_' || EXTRACT(EPOCH FROM NOW())::INTEGER;
INSERT INTO public.users (
id,
email,
full_name,
username,
created_at,
updated_at
) VALUES (
NEW.id,
NEW.email,
user_full_name,
user_username,
NOW(),
NOW()
);
RAISE NOTICE 'User profile created with unique username: %', user_username;
RETURN NEW;
WHEN OTHERS THEN
-- Log error but don't fail the auth creation
RAISE WARNING 'Failed to create user profile for %: %', NEW.email, SQLERRM;
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Drop existing trigger if it exists
DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
-- Create new trigger
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
-- Create index for better performance
CREATE INDEX IF NOT EXISTS users_email_idx ON public.users(email);
CREATE INDEX IF NOT EXISTS users_username_idx ON public.users(username);
-- Grant necessary permissions
GRANT USAGE ON SCHEMA public TO authenticated;
GRANT ALL ON public.users TO authenticated;
GRANT ALL ON public.users TO service_role;
-- Test the setup
DO $$
BEGIN
RAISE NOTICE 'Users table setup completed successfully!';
RAISE NOTICE 'Table exists: %', (SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'users'));
RAISE NOTICE 'RLS enabled: %', (SELECT relrowsecurity FROM pg_class WHERE relname = 'users');
END $$;