-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_setup.sql
More file actions
83 lines (71 loc) · 2.93 KB
/
auth_setup.sql
File metadata and controls
83 lines (71 loc) · 2.93 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
-- Authentication Setup for WatAI Oliver
-- Run this SQL in your Supabase SQL editor to set up authentication tables
-- Create users table for user profiles
CREATE TABLE IF NOT EXISTS users (
user_id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
email VARCHAR NOT NULL UNIQUE,
username VARCHAR NOT NULL,
full_name VARCHAR,
role VARCHAR NOT NULL DEFAULT 'student' CHECK (role IN ('student', 'instructor', 'admin')),
avatar_url VARCHAR,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create updated_at trigger function
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
-- Create trigger for updated_at
DROP TRIGGER IF EXISTS update_users_updated_at ON users;
CREATE TRIGGER update_users_updated_at
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Enable Row Level Security
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
-- Create policies for users table
DROP POLICY IF EXISTS "Users can view their own profile" ON users;
CREATE POLICY "Users can view their own profile" ON users
FOR SELECT USING (auth.uid() = user_id);
DROP POLICY IF EXISTS "Users can update their own profile" ON users;
CREATE POLICY "Users can update their own profile" ON users
FOR UPDATE USING (auth.uid() = user_id);
DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON users;
CREATE POLICY "Enable insert for authenticated users only" ON users
FOR INSERT WITH CHECK (auth.uid() = user_id);
DROP POLICY IF EXISTS "Admins can view all users" ON users;
CREATE POLICY "Admins can view all users" ON users
FOR SELECT USING (
EXISTS (
SELECT 1 FROM users
WHERE user_id = auth.uid()
AND role = 'admin'
)
);
DROP POLICY IF EXISTS "Admins can update all users" ON users;
CREATE POLICY "Admins can update all users" ON users
FOR UPDATE USING (
EXISTS (
SELECT 1 FROM users
WHERE user_id = auth.uid()
AND role = 'admin'
)
);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_users_role ON users(role);
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users(created_at);
-- Insert default admin user (optional - update email as needed)
-- INSERT INTO auth.users (id, email, email_confirmed_at, created_at, updated_at)
-- VALUES (gen_random_uuid(), '[email protected]', NOW(), NOW(), NOW())
-- ON CONFLICT (email) DO NOTHING;
-- Uncomment and update the following to create a default admin user:
-- INSERT INTO users (user_id, email, username, full_name, role)
-- SELECT id, '[email protected]', 'admin', 'System Administrator', 'admin'
-- FROM auth.users
-- WHERE email = '[email protected]'
-- ON CONFLICT (user_id) DO NOTHING;