117 lines
3.0 KiB
TypeScript
117 lines
3.0 KiB
TypeScript
/**
|
|
* Netlify Function: Request Password Reset
|
|
* POST /request-password-reset - Solicita reset de senha via email (público)
|
|
* Não requer autenticação - endpoint público
|
|
*/
|
|
|
|
import type { Handler, HandlerEvent } from "@netlify/functions";
|
|
|
|
const SUPABASE_URL = "https://yuanqfswhberkoevtmfr.supabase.co";
|
|
const SUPABASE_ANON_KEY =
|
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
|
|
|
|
interface PasswordResetRequest {
|
|
email: string;
|
|
redirect_url?: string;
|
|
}
|
|
|
|
export const handler: Handler = async (event: HandlerEvent) => {
|
|
const headers = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
|
"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 body: PasswordResetRequest = JSON.parse(event.body || "{}");
|
|
|
|
console.log("[request-password-reset] Recebido:", {
|
|
email: body.email,
|
|
hasRedirectUrl: !!body.redirect_url,
|
|
});
|
|
|
|
if (!body.email) {
|
|
return {
|
|
statusCode: 400,
|
|
headers,
|
|
body: JSON.stringify({ error: "Email é obrigatório" }),
|
|
};
|
|
}
|
|
|
|
// Chama a API do Supabase para enviar email de reset
|
|
const response = await fetch(`${SUPABASE_URL}/auth/v1/recover`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
apikey: SUPABASE_ANON_KEY,
|
|
},
|
|
body: JSON.stringify({
|
|
email: body.email,
|
|
options: {
|
|
redirectTo:
|
|
body.redirect_url ||
|
|
"https://mediconnectbrasil.netlify.app/reset-password",
|
|
},
|
|
}),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
console.log("[request-password-reset] Resposta Supabase:", {
|
|
status: response.status,
|
|
data,
|
|
});
|
|
|
|
// Supabase sempre retorna 200 mesmo se o email não existir (por segurança)
|
|
if (response.ok) {
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
...headers,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message:
|
|
"Email de reset de senha enviado com sucesso. Verifique sua caixa de entrada.",
|
|
}),
|
|
};
|
|
}
|
|
|
|
return {
|
|
statusCode: response.status,
|
|
headers: {
|
|
...headers,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
} catch (error) {
|
|
console.error("[request-password-reset] Erro:", error);
|
|
return {
|
|
statusCode: 500,
|
|
headers,
|
|
body: JSON.stringify({
|
|
error: "Erro ao solicitar reset de senha",
|
|
details: error instanceof Error ? error.message : "Erro desconhecido",
|
|
}),
|
|
};
|
|
}
|
|
};
|