-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_admin_account.sql
More file actions
133 lines (121 loc) · 4.37 KB
/
create_admin_account.sql
File metadata and controls
133 lines (121 loc) · 4.37 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
-- ============================================================
-- CREATE ADMIN ACCOUNT SCRIPT
-- Learning Management System
-- ============================================================
-- Run this script in the Supabase SQL Editor
-- This will create an admin user with all necessary permissions
-- ============================================================
-- ============================================================
-- IMPORTANT NOTES
-- ============================================================
-- 1. You need to have at least one admin account to manage the system
-- 2. Replace 'admin@example.com' and 'your-strong-password' with your own values
-- 3. Make sure to use a strong password (minimum 8 characters with mixed types)
-- 4. The user will be created with the 'admin' role in the public.users table
-- ============================================================
-- Step 1: Create the user in the auth.users table
-- ============================================================
-- This will create a new user account in the Supabase auth system
-- First, we'll use the supabase.auth.createUser() function to create the user
-- This function is only available in the SQL editor context
DO $$
DECLARE
new_user_id UUID;
BEGIN
-- Create the user in auth.users
INSERT INTO auth.users (
email,
email_confirmed_at,
encrypted_password,
raw_user_meta_data,
created_at,
updated_at
) VALUES (
'admin@example.com',
NOW(),
-- You can generate a proper bcrypt hash using online tools or libraries
-- For testing purposes, you can use the default Supabase password hashing
-- or use a tool like https://bcrypt-generator.com/
-- Example: $2a$10$nTHYVs2a5s0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a
'$2a$10$QvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJQvJ',
'{
"full_name": "System Administrator",
"role": "admin"
}'::jsonb,
NOW(),
NOW()
)
RETURNING id INTO new_user_id;
-- ============================================================
-- Step 2: Create the user in the public.users table
-- ============================================================
-- This will create a corresponding user record in the public.users table
-- with the 'admin' role
INSERT INTO public.users (
id,
email,
full_name,
role,
avatar_url,
is_active,
created_at,
updated_at
) VALUES (
new_user_id,
'admin@example.com',
'System Administrator',
'admin',
NULL,
true,
NOW(),
NOW()
);
-- ============================================================
-- Step 3: Verify the account creation
-- ============================================================
RAISE NOTICE 'Admin account created successfully!';
RAISE NOTICE 'User ID: %', new_user_id;
RAISE NOTICE 'Email: admin@example.com';
RAISE NOTICE 'Role: admin';
EXCEPTION
WHEN unique_violation THEN
RAISE NOTICE 'Admin account with email admin@example.com already exists';
WHEN OTHERS THEN
RAISE NOTICE 'Error creating admin account: %', SQLERRM;
END $$;
-- ============================================================
-- ALTERNATIVE METHOD: USING SUPABASE AUTH API (RECOMMENDED)
-- ============================================================
-- If you prefer to use the Supabase Auth API, you can use this approach:
--
-- 1. Go to your Supabase project dashboard
-- 2. Navigate to "Authentication" > "Users"
-- 3. Click "Add user" button
-- 4. Fill in the details:
-- - Email: admin@example.com
-- - Password: your-strong-password
-- - Confirm password: your-strong-password
-- 5. Click "Create user"
-- 6. Then run the following SQL to update the role to 'admin':
--
-- UPDATE public.users
-- SET role = 'admin', full_name = 'System Administrator'
-- WHERE email = 'admin@example.com';
-- ============================================================
-- VERIFICATION QUERY
-- ============================================================
-- Run this query to verify the admin account exists
SELECT
id,
email,
role,
full_name,
is_active,
created_at
FROM public.users
WHERE role = 'admin';
-- ============================================================
-- TROUBLESHOOTING
-- ============================================================
-- If you get a "permission denied" error, make sure you're using a
-- PostgreSQL role with sufficient permissions (e.g., postgres or service_role)