128 lines
3.4 KiB
TypeScript
128 lines
3.4 KiB
TypeScript
/**
|
|
* Netlify Function: Delete User (Hard Delete)
|
|
* POST /delete-user - Deleta usuário permanentemente
|
|
* ⚠️ OPERAÇÃO IRREVERSÍVEL - Use com extremo cuidado!
|
|
* Requer permissão de admin ou gestor
|
|
* Usa Admin API do Supabase
|
|
*/
|
|
|
|
import type { Handler, HandlerEvent } from "@netlify/functions";
|
|
|
|
const SUPABASE_URL = "https://yuanqfswhberkoevtmfr.supabase.co";
|
|
const SUPABASE_SERVICE_ROLE_KEY =
|
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc1NDk1NDM2OSwiZXhwIjoyMDcwNTMwMzY5fQ.Dez8PQkV8vWv7VkL_fZe-lY-Xs9P5VptNvRRnhkxoXw";
|
|
|
|
export const handler: Handler = async (event: HandlerEvent) => {
|
|
const headers = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Headers": "Content-Type, Authorization, apikey",
|
|
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
};
|
|
|
|
if (event.httpMethod === "OPTIONS") {
|
|
return {
|
|
statusCode: 200,
|
|
headers,
|
|
body: "",
|
|
};
|
|
}
|
|
|
|
if (event.httpMethod !== "POST") {
|
|
return {
|
|
statusCode: 405,
|
|
headers,
|
|
body: JSON.stringify({ error: "Method Not Allowed" }),
|
|
};
|
|
}
|
|
|
|
try {
|
|
const authHeader =
|
|
event.headers.authorization || event.headers.Authorization;
|
|
|
|
if (!authHeader) {
|
|
return {
|
|
statusCode: 401,
|
|
headers,
|
|
body: JSON.stringify({
|
|
error: "Token de autenticação é obrigatório",
|
|
}),
|
|
};
|
|
}
|
|
|
|
const body = JSON.parse(event.body || "{}");
|
|
|
|
console.log("[delete-user] ATENÇÃO: Tentativa de hard delete:", {
|
|
userId: body.userId,
|
|
requestedBy: "via Netlify Function",
|
|
});
|
|
|
|
// Validação
|
|
if (!body.userId) {
|
|
return {
|
|
statusCode: 400,
|
|
headers,
|
|
body: JSON.stringify({
|
|
error: "userId é obrigatório",
|
|
}),
|
|
};
|
|
}
|
|
|
|
// TODO: Aqui deveria verificar se o usuário tem permissão de admin/gestor
|
|
// Verificando o token JWT que foi passado
|
|
|
|
// Deletar usuário via Admin API do Supabase
|
|
const response = await fetch(
|
|
`${SUPABASE_URL}/auth/v1/admin/users/${body.userId}`,
|
|
{
|
|
method: "DELETE",
|
|
headers: {
|
|
apikey: SUPABASE_SERVICE_ROLE_KEY,
|
|
Authorization: `Bearer ${SUPABASE_SERVICE_ROLE_KEY}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
);
|
|
|
|
if (response.ok) {
|
|
console.log("[delete-user] Usuário deletado com sucesso:", body.userId);
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
...headers,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: "Usuário deletado permanentemente",
|
|
userId: body.userId,
|
|
}),
|
|
};
|
|
}
|
|
|
|
const errorData = await response.json();
|
|
console.error("[delete-user] Erro ao deletar:", errorData);
|
|
|
|
return {
|
|
statusCode: response.status,
|
|
headers: {
|
|
...headers,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
error: errorData.msg || errorData.message || "Erro ao deletar usuário",
|
|
details: errorData,
|
|
}),
|
|
};
|
|
} catch (error) {
|
|
console.error("[delete-user] Erro:", error);
|
|
return {
|
|
statusCode: 500,
|
|
headers,
|
|
body: JSON.stringify({
|
|
error: "Erro interno no servidor",
|
|
message: error instanceof Error ? error.message : "Erro desconhecido",
|
|
}),
|
|
};
|
|
}
|
|
};
|