-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-auth.sql
More file actions
28 lines (24 loc) · 817 Bytes
/
Copy pathsetup-auth.sql
File metadata and controls
28 lines (24 loc) · 817 Bytes
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
-- Create a table to track admin users
CREATE TABLE IF NOT EXISTS admins (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
email TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create a secure function to check if a user is an admin
CREATE OR REPLACE FUNCTION is_admin()
RETURNS BOOLEAN AS $$
BEGIN
RETURN EXISTS (
SELECT 1 FROM admins
WHERE id = auth.uid()
);
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Create a row level security policy for the contacts table
ALTER TABLE contacts ENABLE ROW LEVEL SECURITY;
-- Only admins can view contacts
CREATE POLICY "Admins can view contacts" ON contacts
FOR SELECT USING (is_admin());
-- Only admins can update contacts
CREATE POLICY "Admins can update contacts" ON contacts
FOR UPDATE USING (is_admin());