英文:
SUPABASE local development seed users table
问题
I'm working on a SQL function to fill the auth.users table with a given number of rows for local development. However, I didn't find a way to handle password hash in encrypted_password column. I tried to manually register a user with, let's say 12345678
and hardcode the hash in all rows but as expected, it didn't work. I was hoping to get a better understanding of the way supabase encrypts passwords and maybe find a way to mockup a large number of users and still be able to log in as any of them.
以下是要翻译的内容:
我正在编写一个用于在本地开发中填充auth.users表的SQL函数,以给定的行数。但是,我没有找到在encrypted_password列中处理密码哈希的方法。我尝试手动注册一个用户,比如12345678
,并在所有行中硬编码哈希,但如预期的那样,它不起作用。我希望更好地了解supabase加密密码的方式,并找到一种方法来模拟大量用户,仍然能够以任何用户登录。
希望这可以帮助:
BEGIN;
WITH user_values AS (
SELECT
uuid_generate_v4() AS id,
uuid_generate_v4() AS instance_id,
'authenticated' AS aud,
'authenticated' AS role,
(ROW_NUMBER() OVER ()) || '@gmail.com' AS email,
'$2a$10$nc8ek4I97q1rDN6w3jKj8uUuKGF1fMlfKaD1GNPtYDwhW6IVzA39i' AS encrypted_password,
now() AS email_confirmed_at,
NULL::timestamp AS invited_at,
'' AS confirmation_token,
NULL::timestamp AS confirmation_sent_at,
'' AS recovery_token,
NULL::timestamp AS recovery_sent_at,
'' AS email_change_token_new,
'' AS email_change,
NULL::timestamp AS email_change_sent_at,
NULL::timestamp AS last_sign_in_at,
'{"provider":"email","providers":["email"]}'::jsonb AS raw_app_meta_data,
'{}'::jsonb AS raw_user_meta_data,
0::boolean AS is_super_admin,
'2022-10-04 03:41:27.391146+00'::timestamp AS created_at,
'2022-10-04 03:41:27.391146+00'::timestamp AS updated_at,
NULL AS phone,
NULL::timestamp AS phone_confirmed_at,
'' AS phone_change,
'' AS phone_change_token,
NULL::timestamp AS phone_change_sent_at,
'' AS email_change_token_current,
0 AS email_change_confirm_status,
NULL::timestamp AS banned_until,
'' AS reauthentication_token,
NULL::timestamp AS reauthentication_sent_at
FROM generate_series(1, 100)
),
inserted_users AS (
INSERT INTO auth.users (
id,
instance_id,
aud,
role,
email,
encrypted_password,
email_confirmed_at,
invited_at,
confirmation_token,
confirmation_sent_at,
recovery_token,
recovery_sent_at,
email_change_token_new,
email_change,
email_change_sent_at,
last_sign_in_at,
raw_app_meta_data,
raw_user_meta_data,
is_super_admin,
created_at,
updated_at,
phone,
phone_confirmed_at,
phone_change,
phone_change_token,
phone_change_sent_at,
email_change_token_current,
email_change_confirm_status,
banned_until,
reauthentication_token,
reauthentication_sent_at
)
SELECT * FROM user_values RETURNING id, instance_id
)
INSERT INTO public.user_profile (
id,
created_at,
avatar_url,
username,
country_id
)
SELECT
id,
now(),
'',
'USUARIO' || ROW_NUMBER() OVER (),
20
FROM inserted_users;
COMMIT;
(Note: I've provided the translation of the text and the SQL script.)
英文:
I'm working on a SQL function to fill the auth.users table with a given number of rows for local development.
I manage to complete the creation function but I didn't find a way to handle password hash in encrypted_password column. I tried to manually register a user with, let's say 12345678
and hardcode the hash in all rows but as expected, it didn't work.
I was hoping to get a better understanding of the way supabase encrypts passwords and maybe find a way to mokcup a large number of users and still be able to login as any of them.
However, this is my scropt in case it helps:
BEGIN;
WITH user_values AS (
SELECT
uuid_generate_v4() AS id,
uuid_generate_v4() AS instance_id,
'authenticated' AS aud,
'authenticated' AS role,
(ROW_NUMBER() OVER ()) || '@gmail.com' AS email,
'$2a$10$nc8ek4I97q1rDN6w3jKj8uUuKGF1fMlfKaD1GNPtYDwhW6IVzA39i' AS encrypted_password,
now() AS email_confirmed_at,
NULL::timestamp AS invited_at,
'' AS confirmation_token,
NULL::timestamp AS confirmation_sent_at,
'' AS recovery_token,
NULL::timestamp AS recovery_sent_at,
'' AS email_change_token_new,
'' AS email_change,
NULL::timestamp AS email_change_sent_at,
NULL::timestamp AS last_sign_in_at,
'{"provider":"email","providers":["email"]}'::jsonb AS raw_app_meta_data,
'{}'::jsonb AS raw_user_meta_data,
0::boolean AS is_super_admin,
'2022-10-04 03:41:27.391146+00'::timestamp AS created_at,
'2022-10-04 03:41:27.391146+00'::timestamp AS updated_at,
NULL AS phone,
NULL::timestamp AS phone_confirmed_at,
'' AS phone_change,
'' AS phone_change_token,
NULL::timestamp AS phone_change_sent_at,
'' AS email_change_token_current,
0 AS email_change_confirm_status,
NULL::timestamp AS banned_until,
'' AS reauthentication_token,
NULL::timestamp AS reauthentication_sent_at
FROM generate_series(1, 100)
),
inserted_users AS (
INSERT INTO auth.users (
id,
instance_id,
aud,
role,
email,
encrypted_password,
email_confirmed_at,
invited_at,
confirmation_token,
confirmation_sent_at,
recovery_token,
recovery_sent_at,
email_change_token_new,
email_change,
email_change_sent_at,
last_sign_in_at,
raw_app_meta_data,
raw_user_meta_data,
is_super_admin,
created_at,
updated_at,
phone,
phone_confirmed_at,
phone_change,
phone_change_token,
phone_change_sent_at,
email_change_token_current,
email_change_confirm_status,
banned_until,
reauthentication_token,
reauthentication_sent_at
)
SELECT * FROM user_values RETURNING id, instance_id
)
INSERT INTO public.user_profile (
id,
created_at,
avatar_url,
username,
country_id
)
SELECT
id,
now(),
'',
'USUARIO' || ROW_NUMBER() OVER (),
20
FROM inserted_users;
COMMIT;
答案1
得分: 1
这个函数可以生成表格auth.users中所需数量的用户,只要提供一个值:
FROM generate_series(1, 100)
它会创建一个以{row}@gmail.com
为邮箱和您传递的密码为密码的用户,如下所示:
crypt('password123', gen_salt('bf'))
适用于本地开发和测试
如果您在本地工作,可以将此脚本直接放入您的seed.sql
文件中,或者在Supabase仪表板的SQL编辑器中运行它,如下所示:
BEGIN;
WITH user_values AS (
SELECT
uuid_generate_v4() AS id,
'00000000-0000-0000-0000-000000000000'::uuid AS instance_id,
'authenticated' AS aud,
'authenticated' AS role,
(ROW_NUMBER() OVER ()) || '@gmail.com' AS email,
crypt('password123', gen_salt('bf')) AS encrypted_password,
now() AS email_confirmed_at,
NULL::timestamp AS invited_at,
'' AS confirmation_token,
NULL::timestamp AS confirmation_sent_at,
'' AS recovery_token,
NULL::timestamp AS recovery_sent_at,
'' AS email_change_token_new,
'' AS email_change,
NULL::timestamp AS email_change_sent_at,
now()::timestamp AS last_sign_in_at,
'{"provider":"email","providers":["email"]}'::jsonb AS raw_app_meta_data,
'{}'::jsonb AS raw_user_meta_data,
0::boolean AS is_super_admin,
'2022-10-04 03:41:27.391146+00'::timestamp AS created_at,
'2022-10-04 03:41:27.391146+00'::timestamp AS updated_at,
NULL AS phone,
NULL::timestamp AS phone_confirmed_at,
'' AS phone_change,
'' AS phone_change_token,
NULL::timestamp AS phone_change_sent_at,
'' AS email_change_token_current,
0 AS email_change_confirm_status,
NULL::timestamp AS banned_until,
'' AS reauthentication_token,
NULL::timestamp AS reauthentication_sent_at
FROM generate_series(1, 100)
),
inserted_users AS (
INSERT INTO auth.users (
id,
instance_id,
aud,
role,
email,
encrypted_password,
email_confirmed_at,
invited_at,
confirmation_token,
confirmation_sent_at,
recovery_token,
recovery_sent_at,
email_change_token_new,
email_change,
email_change_sent_at,
last_sign_in_at,
raw_app_meta_data,
raw_user_meta_data,
is_super_admin,
created_at,
updated_at,
phone,
phone_confirmed_at,
phone_change,
phone_change_token,
phone_change_sent_at,
email_change_token_current,
email_change_confirm_status,
banned_until,
reauthentication_token,
reauthentication_sent_at
)
SELECT * FROM user_values RETURNING id, instance_id
)
--(OPTIONAL) 这里您可以将创建的用户插入另一个表中
-- INSERT INTO public.user_profile (
-- id,
-- created_at,
-- username
-- )
-- SELECT
-- id,
-- now(),
-- 'USUARIO' || ROW_NUMBER() OVER (),
-- FROM inserted_users;
COMMIT;
英文:
This function generates as many users as you want for the table auth.users, given a value in:
FROM generate_series(1, 100)
It will create a user with email {row}@gmail.com
and the passsword you pass to:
crypt('password123', gen_salt('bf'))
Usefull for local development and testing
You can directly put the script into your seed.sql
file if you're working locally, or run it with the SQL editor in the Supabase dashboard.
BEGIN;
WITH user_values AS (
SELECT
uuid_generate_v4() AS id,
'00000000-0000-0000-0000-000000000000'::uuid AS instance_id,
'authenticated' AS aud,
'authenticated' AS role,
(ROW_NUMBER() OVER ()) || '@gmail.com' AS email,
crypt('password123', gen_salt('bf')) AS encrypted_password,
now() AS email_confirmed_at,
NULL::timestamp AS invited_at,
'' AS confirmation_token,
NULL::timestamp AS confirmation_sent_at,
'' AS recovery_token,
NULL::timestamp AS recovery_sent_at,
'' AS email_change_token_new,
'' AS email_change,
NULL::timestamp AS email_change_sent_at,
now()::timestamp AS last_sign_in_at,
'{"provider":"email","providers":["email"]}'::jsonb AS raw_app_meta_data,
'{}'::jsonb AS raw_user_meta_data,
0::boolean AS is_super_admin,
'2022-10-04 03:41:27.391146+00'::timestamp AS created_at,
'2022-10-04 03:41:27.391146+00'::timestamp AS updated_at,
NULL AS phone,
NULL::timestamp AS phone_confirmed_at,
'' AS phone_change,
'' AS phone_change_token,
NULL::timestamp AS phone_change_sent_at,
'' AS email_change_token_current,
0 AS email_change_confirm_status,
NULL::timestamp AS banned_until,
'' AS reauthentication_token,
NULL::timestamp AS reauthentication_sent_at
FROM generate_series(1, 100)
),
inserted_users AS (
INSERT INTO auth.users (
id,
instance_id,
aud,
role,
email,
encrypted_password,
email_confirmed_at,
invited_at,
confirmation_token,
confirmation_sent_at,
recovery_token,
recovery_sent_at,
email_change_token_new,
email_change,
email_change_sent_at,
last_sign_in_at,
raw_app_meta_data,
raw_user_meta_data,
is_super_admin,
created_at,
updated_at,
phone,
phone_confirmed_at,
phone_change,
phone_change_token,
phone_change_sent_at,
email_change_token_current,
email_change_confirm_status,
banned_until,
reauthentication_token,
reauthentication_sent_at
)
SELECT * FROM user_values RETURNING id, instance_id
)
--(OPTINAL) Here you can inser the created users into another table
-- INSERT INTO public.user_profile (
-- id,
-- created_at,
-- username
-- )
-- SELECT
-- id,
-- now(),
-- 'USUARIO' || ROW_NUMBER() OVER (),
-- FROM inserted_users;
COMMIT;
答案2
得分: 0
-- 创建测试用户
INSERT INTO
auth.users (
instance_id,
id,
aud,
role,
email,
encrypted_password,
email_confirmed_at,
recovery_sent_at,
last_sign_in_at,
raw_app_meta_data,
raw_user_meta_data,
created_at,
updated_at,
confirmation_token,
email_change,
email_change_token_new,
recovery_token
) (
select
'00000000-0000-0000-0000-000000000000',
uuid_generate_v4(),
'authenticated',
'authenticated',
'user' || (ROW_NUMBER() OVER ()) || '@example.com',
crypt('password123', gen_salt('bf')),
current_timestamp,
current_timestamp,
current_timestamp,
'{"provider":"email","providers":["email"]}',
'{}',
current_timestamp,
current_timestamp,
'',
'',
'',
''
FROM
generate_series(1, 10)
);
-- 测试用户电子邮件身份
INSERT INTO
auth.identities (
id,
user_id,
identity_data,
provider,
last_sign_in_at,
created_at,
updated_at
) (
select
uuid_generate_v4(),
id,
format('{"sub":"%s","email":"%s"}', id::text, email)::jsonb,
'email',
current_timestamp,
current_timestamp,
current_timestamp
from
auth.users
);
这是您提供的 SQL 代码的中文翻译。
英文:
-- create test users
INSERT INTO
auth.users (
instance_id,
id,
aud,
role,
email,
encrypted_password,
email_confirmed_at,
recovery_sent_at,
last_sign_in_at,
raw_app_meta_data,
raw_user_meta_data,
created_at,
updated_at,
confirmation_token,
email_change,
email_change_token_new,
recovery_token
) (
select
'00000000-0000-0000-0000-000000000000',
uuid_generate_v4 (),
'authenticated',
'authenticated',
'user' || (ROW_NUMBER() OVER ()) || '@example.com',
crypt ('password123', gen_salt ('bf')),
current_timestamp,
current_timestamp,
current_timestamp,
'{"provider":"email","providers":["email"]}',
'{}',
current_timestamp,
current_timestamp,
'',
'',
'',
''
FROM
generate_series(1, 10)
);
-- test user email identities
INSERT INTO
auth.identities (
id,
user_id,
identity_data,
provider,
last_sign_in_at,
created_at,
updated_at
) (
select
uuid_generate_v4 (),
id,
format('{"sub":"%s","email":"%s"}', id::text, email)::jsonb,
'email',
current_timestamp,
current_timestamp,
current_timestamp
from
auth.users
);
This creates 10 users generate_series(1, 10)
Same passwords for all users. You may salt it if you prefer.
Here is the gist
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论