fix: manage-users — remove manual public.users insert, trigger handle_new_user does it via user_metadata
This commit is contained in:
parent
cadcceef7e
commit
93d3ce4ca8
|
|
@ -81,17 +81,6 @@ Deno.serve(async (request) => {
|
||||||
return jsonResponse({ ok: false, error: "Роль обязательна" }, 400, corsHeaders);
|
return jsonResponse({ ok: false, error: "Роль обязательна" }, 400, corsHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve role_id from roles table
|
|
||||||
const { data: roleRow, error: roleErr } = await supabase
|
|
||||||
.from("roles")
|
|
||||||
.select("id")
|
|
||||||
.eq("name", role)
|
|
||||||
.maybeSingle();
|
|
||||||
if (roleErr || !roleRow) {
|
|
||||||
return jsonResponse({ ok: false, error: "Неизвестная роль: " + role }, 400, corsHeaders);
|
|
||||||
}
|
|
||||||
const roleId = roleRow.id;
|
|
||||||
|
|
||||||
// Check if email already exists in public.users
|
// Check if email already exists in public.users
|
||||||
const { data: existingUser } = await supabase
|
const { data: existingUser } = await supabase
|
||||||
.from("users")
|
.from("users")
|
||||||
|
|
@ -102,17 +91,18 @@ Deno.serve(async (request) => {
|
||||||
return jsonResponse({ ok: false, error: "Пользователь с таким email уже существует" }, 409, corsHeaders);
|
return jsonResponse({ ok: false, error: "Пользователь с таким email уже существует" }, 409, corsHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create auth user
|
// Create auth user — trigger handle_new_user will auto-insert into public.users
|
||||||
|
// using user_metadata.role and user_metadata.name
|
||||||
const { data: authData, error: authError } = await supabase.auth.admin.createUser({
|
const { data: authData, error: authError } = await supabase.auth.admin.createUser({
|
||||||
email,
|
email,
|
||||||
email_confirm: true,
|
email_confirm: true,
|
||||||
user_metadata: { name },
|
user_metadata: { name, role },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (authError) {
|
if (authError) {
|
||||||
console.error("auth.createUser error:", authError);
|
console.error("auth.createUser error:", authError);
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ ok: false, error: "Ошибка создания auth-пользователя: " + authError.message },
|
{ ok: false, error: "Ошибка создания пользователя: " + authError.message },
|
||||||
500,
|
500,
|
||||||
corsHeaders,
|
corsHeaders,
|
||||||
);
|
);
|
||||||
|
|
@ -120,25 +110,6 @@ Deno.serve(async (request) => {
|
||||||
|
|
||||||
const newUserId = authData.user.id;
|
const newUserId = authData.user.id;
|
||||||
|
|
||||||
// Insert into public.users
|
|
||||||
const { error: usersInsertError } = await supabase.from("users").insert({
|
|
||||||
id: newUserId,
|
|
||||||
email,
|
|
||||||
name,
|
|
||||||
role_id: roleId,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (usersInsertError) {
|
|
||||||
console.error("users.insert error:", usersInsertError);
|
|
||||||
// Rollback auth user
|
|
||||||
await supabase.auth.admin.deleteUser(newUserId);
|
|
||||||
return jsonResponse(
|
|
||||||
{ ok: false, error: "Ошибка создания записи пользователя: " + usersInsertError.message },
|
|
||||||
500,
|
|
||||||
corsHeaders,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return jsonResponse({ ok: true, data: { id: newUserId, email, name, role } }, 201, corsHeaders);
|
return jsonResponse({ ok: true, data: { id: newUserId, email, name, role } }, 201, corsHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue