(Feat) V2
This commit is contained in:
@@ -81,6 +81,8 @@ Deno.serve(async (req) => {
|
||||
role: pr.role ?? 'other',
|
||||
roles,
|
||||
is_admin: pr.is_admin ?? false,
|
||||
avatar_color: pr.avatar_color ?? '#7c5cff',
|
||||
avatar_url: pr.avatar_url ?? null,
|
||||
created_at: pr.created_at ?? u.created_at
|
||||
}
|
||||
})
|
||||
@@ -131,14 +133,34 @@ Deno.serve(async (req) => {
|
||||
}
|
||||
|
||||
case 'update': {
|
||||
const { id, full_name, role, roles, is_admin } = p
|
||||
const { id, full_name, email, password, role, roles, is_admin, avatar_url } = p
|
||||
if (!id) return json({ error: 'Identifiant manquant.' }, 400)
|
||||
if (id === user.id && is_admin === false)
|
||||
return json({ error: 'Tu ne peux pas retirer ton propre statut admin.' }, 400)
|
||||
|
||||
// --- a) Champs du compte auth (email / mot de passe / métadonnées) ---
|
||||
const authPatch: Record<string, unknown> = {}
|
||||
if (email !== undefined && String(email).trim()) {
|
||||
authPatch.email = String(email).trim()
|
||||
authPatch.email_confirm = true // évite le mail de confirmation
|
||||
}
|
||||
if (password !== undefined && String(password) !== '') {
|
||||
if (String(password).length < 6)
|
||||
return json({ error: 'Le mot de passe doit faire au moins 6 caractères.' }, 400)
|
||||
authPatch.password = String(password)
|
||||
}
|
||||
if (full_name !== undefined)
|
||||
authPatch.user_metadata = { full_name, role: Array.isArray(roles) ? roles[0] : role }
|
||||
if (Object.keys(authPatch).length > 0) {
|
||||
const { error: authErr } = await admin.auth.admin.updateUserById(id, authPatch)
|
||||
if (authErr) return json({ error: authErr.message }, 400)
|
||||
}
|
||||
|
||||
// --- b) Champs du profil (nom, pôles, admin) ---
|
||||
const patch: Record<string, unknown> = {}
|
||||
if (full_name !== undefined) patch.full_name = full_name
|
||||
if (is_admin !== undefined) patch.is_admin = !!is_admin
|
||||
if (avatar_url !== undefined) patch.avatar_url = avatar_url || null
|
||||
// Gestion des pôles : roles = liste complète, role = principal (roles[0]).
|
||||
if (Array.isArray(roles)) {
|
||||
const primary = roles[0] ?? 'other'
|
||||
@@ -148,10 +170,13 @@ Deno.serve(async (req) => {
|
||||
patch.role = role
|
||||
patch.roles = [role]
|
||||
}
|
||||
if (Object.keys(patch).length === 0) return json({ error: 'Rien à modifier.' }, 400)
|
||||
if (Object.keys(patch).length > 0) {
|
||||
const { error } = await admin.from('profiles').update(patch).eq('id', id)
|
||||
if (error) return json({ error: error.message }, 400)
|
||||
}
|
||||
|
||||
const { error } = await admin.from('profiles').update(patch).eq('id', id)
|
||||
if (error) return json({ error: error.message }, 400)
|
||||
if (Object.keys(authPatch).length === 0 && Object.keys(patch).length === 0)
|
||||
return json({ error: 'Rien à modifier.' }, 400)
|
||||
return json({ ok: true })
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user