feat(F-099): completed feature
This commit is contained in:
@@ -11,3 +11,12 @@ FLAG_EXAMPLE_FEATURE=false
|
||||
# Session cookie Secure flag. Defaults to true (production-safe); set false only
|
||||
# for local http development where browsers reject Secure cookies.
|
||||
COOKIE_SECURE=false
|
||||
|
||||
# Password-reset SMTP (OVH example for mercadodevida.es)
|
||||
PUBLIC_APP_URL=https://mercadodevida.es
|
||||
SMTP_HOST=ssl0.ovh.net
|
||||
SMTP_PORT=465
|
||||
SMTP_SECURE=true
|
||||
SMTP_USER=info@mercadodevida.es
|
||||
SMTP_FROM=info@mercadodevida.es
|
||||
# SMTP_PASS=replace-with-mailbox-password
|
||||
|
||||
@@ -9,6 +9,7 @@ const TABS = [
|
||||
{ id: 'social', label: 'Redes sociales', icon: '🌐' },
|
||||
{ id: 'footer', label: 'Footer', icon: '📄' },
|
||||
{ id: 'ai', label: 'IA para SEO', icon: '✨' },
|
||||
{ id: 'smtp', label: 'SMTP / Email', icon: '✉️' },
|
||||
] as const;
|
||||
type TabId = (typeof TABS)[number]['id'];
|
||||
|
||||
@@ -32,8 +33,12 @@ export default function SettingsPage() {
|
||||
if (!form) return;
|
||||
setSaving(true); setErr(''); setMsg('');
|
||||
try {
|
||||
const { aiApiKey, ...settingsWithoutKey } = form;
|
||||
const updated = await settingsApi.update(aiApiKey ? form : settingsWithoutKey);
|
||||
const { aiApiKey, smtpPass, ...settingsWithoutSecrets } = form;
|
||||
const updated = await settingsApi.update({
|
||||
...settingsWithoutSecrets,
|
||||
...(aiApiKey ? { aiApiKey } : {}),
|
||||
...(smtpPass ? { smtpPass } : {}),
|
||||
});
|
||||
setData(updated); setForm(updated);
|
||||
setMsg('Cambios guardados correctamente');
|
||||
setTimeout(() => setMsg(''), 4000);
|
||||
@@ -44,7 +49,7 @@ export default function SettingsPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const field = (key: Exclude<keyof FormData, 'aiApiKeyConfigured'>, label: string, opts?: { type?: string; placeholder?: string; rows?: number; hint?: string }) => (
|
||||
const field = (key: Exclude<keyof FormData, 'aiApiKeyConfigured' | 'smtpPassConfigured' | 'smtpSecure'>, label: string, opts?: { type?: string; placeholder?: string; rows?: number; hint?: string }) => (
|
||||
<div key={key}>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">{label}</label>
|
||||
{opts?.rows ? (
|
||||
@@ -144,6 +149,27 @@ export default function SettingsPage() {
|
||||
{field('aiApiKey', 'API key', { type: 'password', placeholder: form?.aiApiKeyConfigured ? 'API key configurada (escribe para reemplazar)' : 'sk-...' })}
|
||||
{field('aiSeoTitlePrompt', 'Prompt para Título SEO', { rows: 4, hint: 'Usa {{name}}, {{description}} y {{brand}} como variables.' })}
|
||||
{field('aiSeoDescriptionPrompt', 'Prompt para Descripción SEO (Google)', { rows: 5, hint: 'Usa {{name}}, {{description}} y {{brand}} como variables.' })}
|
||||
{field('aiProductDescriptionPrompt', 'Prompt para Descripción del producto', { rows: 5, hint: 'Se usa solo cuando la descripción normal está vacía. Variables: {{name}}, {{description}} y {{brand}}.' })}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{tab === 'smtp' && (
|
||||
<>
|
||||
<div className="px-6 py-4 bg-gray-50 border-b border-gray-200">
|
||||
<h2 className="text-base font-semibold text-gray-800">Correo SMTP</h2>
|
||||
<p className="text-xs text-gray-400 mt-0.5">Se usa para enviar enlaces de recuperación de contraseña.</p>
|
||||
</div>
|
||||
<div className="p-6 space-y-5">
|
||||
{field('smtpHost', 'Servidor SMTP', { placeholder: 'ssl0.ovh.net' })}
|
||||
{field('smtpPort', 'Puerto', { type: 'number', placeholder: '465' })}
|
||||
<label className="flex items-center gap-2 text-sm text-gray-700">
|
||||
<input type="checkbox" checked={form?.smtpSecure ?? true} onChange={e => setForm(f => f ? { ...f, smtpSecure: e.target.checked } : f)} />
|
||||
Conexión segura SSL/TLS
|
||||
</label>
|
||||
{field('smtpUser', 'Usuario / cuenta de correo', { type: 'email', placeholder: 'info@mercadodevida.es' })}
|
||||
{field('smtpPass', 'Contraseña SMTP', { type: 'password', placeholder: form?.smtpPassConfigured ? 'Contraseña configurada (escribe para reemplazar)' : 'Contraseña del buzón' })}
|
||||
{field('smtpFrom', 'Remitente', { type: 'email', placeholder: 'info@mercadodevida.es' })}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -17,6 +17,16 @@ export async function GET(req: NextRequest) {
|
||||
const backendRes = await fetch(`${API}/${path}`, {
|
||||
headers: { Cookie: cookies },
|
||||
});
|
||||
if (path === 'admin/logs/stream') {
|
||||
return new Response(backendRes.body, {
|
||||
status: backendRes.status,
|
||||
headers: {
|
||||
'Content-Type': backendRes.headers.get('content-type') ?? 'text/event-stream',
|
||||
'Cache-Control': backendRes.headers.get('cache-control') ?? 'no-cache',
|
||||
Connection: 'keep-alive',
|
||||
},
|
||||
});
|
||||
}
|
||||
const data = await backendRes.json().catch(() => null);
|
||||
const resp = NextResponse.json(data ?? { error: 'Bad response' }, { status: backendRes.status });
|
||||
return resp;
|
||||
|
||||
@@ -85,10 +85,9 @@ export function ServerLogViewer({ backendUrl = 'http://192.168.18.93:3000' }: Se
|
||||
|
||||
const connect = async () => {
|
||||
try {
|
||||
// Read cookie from document
|
||||
const cookies = document.cookie;
|
||||
const response = await fetch(`${backendUrl}/admin/logs/stream`, {
|
||||
headers: { Cookie: cookies },
|
||||
// Use the same-origin proxy so the httpOnly backoffice cookie is forwarded server-side.
|
||||
const response = await fetch('/api/admin/logs/stream', {
|
||||
credentials: 'include',
|
||||
});
|
||||
|
||||
if (!response.ok || aborted) {
|
||||
|
||||
@@ -256,18 +256,21 @@ function Toolbar({ disabled }: { disabled?: boolean }) {
|
||||
|
||||
function InitialHtmlPlugin({ initialHtml }: { initialHtml: string }) {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
const applied = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (applied.current) return;
|
||||
if (!initialHtml || !initialHtml.trim()) return;
|
||||
applied.current = true;
|
||||
let currentHtml = '';
|
||||
editor.getEditorState().read(() => {
|
||||
currentHtml = $generateHtmlFromNodes(editor, null);
|
||||
});
|
||||
if (currentHtml.trim() === initialHtml.trim()) return;
|
||||
|
||||
editor.update(() => {
|
||||
const root = $getRoot();
|
||||
root.clear();
|
||||
if (!initialHtml.trim()) return;
|
||||
const parser = new DOMParser();
|
||||
const domDoc = parser.parseFromString(`<div>${initialHtml}</div>`, 'text/html');
|
||||
const nodes = $generateNodesFromDOM(editor, domDoc.body);
|
||||
const root = $getRoot();
|
||||
root.clear();
|
||||
const ensured: ElementNode[] = [];
|
||||
nodes.forEach((node) => {
|
||||
if ($isElementNode(node)) {
|
||||
@@ -278,9 +281,7 @@ function InitialHtmlPlugin({ initialHtml }: { initialHtml: string }) {
|
||||
ensured.push(p);
|
||||
}
|
||||
});
|
||||
if (ensured.length === 0) {
|
||||
ensured.push($createParagraphNode());
|
||||
}
|
||||
if (ensured.length === 0) ensured.push($createParagraphNode());
|
||||
ensured.forEach((n) => root.append(n));
|
||||
});
|
||||
}, [editor, initialHtml]);
|
||||
|
||||
@@ -42,6 +42,10 @@ function slugify(text: string): string {
|
||||
return text.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
|
||||
}
|
||||
|
||||
function hasMeaningfulContent(value: string): boolean {
|
||||
return value.replace(/<[^>]*>/g, '').replace(/ | /gi, ' ').trim().length > 0;
|
||||
}
|
||||
|
||||
export function ProductEditor({ productId }: ProductEditorProps) {
|
||||
const router = useRouter();
|
||||
const isCreate = !productId;
|
||||
@@ -111,7 +115,8 @@ export function ProductEditor({ productId }: ProductEditorProps) {
|
||||
setState(p.state);
|
||||
setSeoTitle((p as any).seoTitle ?? ''); setSeoTitleManual(true);
|
||||
setSeoDesc((p as any).seoDescription ?? ''); setSeoDescManual(true);
|
||||
setExpirationDate((p as any).expirationDate ?? '');
|
||||
const rawExpirationDate = String((p as any).expirationDate ?? '');
|
||||
setExpirationDate(rawExpirationDate ? rawExpirationDate.slice(0, 10) : '');
|
||||
snapRef.current = getSnapRef.current();
|
||||
setLoading(false);
|
||||
}).catch(() => {
|
||||
@@ -145,7 +150,7 @@ export function ProductEditor({ productId }: ProductEditorProps) {
|
||||
try {
|
||||
const payload = {
|
||||
name, slug,
|
||||
description: desc || undefined,
|
||||
description: hasMeaningfulContent(desc) ? desc.trim() : null,
|
||||
brandId: brandId || undefined,
|
||||
categoryIds,
|
||||
channels,
|
||||
@@ -159,9 +164,10 @@ export function ProductEditor({ productId }: ProductEditorProps) {
|
||||
let saved: Product;
|
||||
if (isCreate) saved = await productsApi.create(payload);
|
||||
else saved = await productsApi.update(productId, payload);
|
||||
if (!seoTitle.trim() || !seoDesc.trim()) {
|
||||
if (!hasMeaningfulContent(desc) || !seoTitle.trim() || !seoDesc.trim()) {
|
||||
try {
|
||||
saved = await productsApi.generateSeo(saved.id);
|
||||
setDesc(saved.description ?? '');
|
||||
setSeoTitle(saved.seoTitle ?? '');
|
||||
setSeoDesc(saved.seoDescription ?? '');
|
||||
} catch (generationError) {
|
||||
|
||||
@@ -27,10 +27,12 @@ async function request<T>(method: string, path: string, body?: unknown): Promise
|
||||
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({ message: 'Request failed' }));
|
||||
const envelope = body as { code?: string; message?: string; error?: { code?: string; message?: string } };
|
||||
const error = envelope.error ?? envelope;
|
||||
throw new ApiError(
|
||||
res.status,
|
||||
(body as { code?: string }).code ?? 'REQUEST_FAILED',
|
||||
(body as { message?: string }).message ?? 'Request failed',
|
||||
error.code ?? 'REQUEST_FAILED',
|
||||
error.message ?? 'Request failed',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -334,6 +336,14 @@ export interface StoreSettings {
|
||||
aiApiKeyConfigured?: boolean;
|
||||
aiSeoTitlePrompt: string;
|
||||
aiSeoDescriptionPrompt: string;
|
||||
aiProductDescriptionPrompt: string;
|
||||
smtpHost: string;
|
||||
smtpPort: string;
|
||||
smtpSecure: boolean;
|
||||
smtpUser: string;
|
||||
smtpPass: string;
|
||||
smtpPassConfigured?: boolean;
|
||||
smtpFrom: string;
|
||||
}
|
||||
|
||||
export const settingsApi = {
|
||||
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
|
After Width: | Height: | Size: 8.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 950 B |
Binary file not shown.
|
After Width: | Height: | Size: 146 KiB |
@@ -4,7 +4,12 @@ import { useState, useEffect, useCallback } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useAuth } from '@/contexts/AuthContext';
|
||||
|
||||
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
|
||||
interface Profile {
|
||||
id: string;
|
||||
email: string;
|
||||
displayName: string | null;
|
||||
phone: string | null;
|
||||
}
|
||||
|
||||
interface Address {
|
||||
id: string;
|
||||
@@ -62,8 +67,8 @@ function AddressForm({
|
||||
setError('');
|
||||
try {
|
||||
const url = initial
|
||||
? `${API}/users/${userId}/addresses/${initial.id}`
|
||||
: `${API}/users/${userId}/addresses`;
|
||||
? `/api/users/${userId}/addresses/${initial.id}`
|
||||
: `/api/users/${userId}/addresses`;
|
||||
const method = initial ? 'PATCH' : 'POST';
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
@@ -187,6 +192,14 @@ export default function AccountPage() {
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [editing, setEditing] = useState<Address | null>(null);
|
||||
const [msg, setMsg] = useState('');
|
||||
const [profile, setProfile] = useState<Profile | null>(null);
|
||||
const [profileForm, setProfileForm] = useState({ displayName: '', phone: '' });
|
||||
const [profileSaving, setProfileSaving] = useState(false);
|
||||
const [profileError, setProfileError] = useState('');
|
||||
const [passwordForm, setPasswordForm] = useState({ currentPassword: '', newPassword: '', confirmPassword: '' });
|
||||
const [passwordSaving, setPasswordSaving] = useState(false);
|
||||
const [passwordMessage, setPasswordMessage] = useState('');
|
||||
const [passwordError, setPasswordError] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
if (!authLoading && !user) {
|
||||
@@ -198,7 +211,7 @@ export default function AccountPage() {
|
||||
if (!user) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await fetch(`${API}/users/${user.id}/addresses`, { credentials: 'include' });
|
||||
const res = await fetch(`/api/users/${user.id}/addresses`, { credentials: 'include' });
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
setAddresses(data.items ?? []);
|
||||
@@ -211,16 +224,58 @@ export default function AccountPage() {
|
||||
}, [user]);
|
||||
|
||||
useEffect(() => {
|
||||
const timer = window.setTimeout(() => {
|
||||
void load();
|
||||
}, 0);
|
||||
return () => window.clearTimeout(timer);
|
||||
}, [load]);
|
||||
if (!user) return;
|
||||
fetch(`/api/users/${user.id}`, { credentials: 'include' })
|
||||
.then((res) => res.ok ? res.json() : null)
|
||||
.then((data: Profile | null) => {
|
||||
if (!data) return;
|
||||
setProfile(data);
|
||||
setProfileForm({ displayName: data.displayName ?? '', phone: data.phone ?? '' });
|
||||
})
|
||||
.catch(() => setProfileError('No se pudieron cargar tus datos personales'));
|
||||
}, [user]);
|
||||
|
||||
const saveProfile = async (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
if (!user) return;
|
||||
setProfileSaving(true); setProfileError('');
|
||||
try {
|
||||
const res = await fetch(`/api/users/${user.id}`, {
|
||||
method: 'PATCH', headers: { 'Content-Type': 'application/json' }, credentials: 'include',
|
||||
body: JSON.stringify({ displayName: profileForm.displayName, phone: profileForm.phone }),
|
||||
});
|
||||
const data = await res.json().catch(() => null);
|
||||
if (!res.ok) throw new Error(data?.error?.message ?? 'No se pudieron guardar los datos');
|
||||
setProfile(data); setMsg('Datos personales actualizados');
|
||||
} catch (error) {
|
||||
setProfileError(error instanceof Error ? error.message : 'No se pudieron guardar los datos');
|
||||
} finally { setProfileSaving(false); }
|
||||
};
|
||||
|
||||
const changePassword = async (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
setPasswordError(''); setPasswordMessage('');
|
||||
if (passwordForm.newPassword.length < 8) { setPasswordError('La nueva contraseña debe tener al menos 8 caracteres.'); return; }
|
||||
if (passwordForm.newPassword !== passwordForm.confirmPassword) { setPasswordError('Las contraseñas nuevas no coinciden.'); return; }
|
||||
setPasswordSaving(true);
|
||||
try {
|
||||
const res = await fetch('/api/auth/me/password', {
|
||||
method: 'PATCH', headers: { 'Content-Type': 'application/json' }, credentials: 'include',
|
||||
body: JSON.stringify({ currentPassword: passwordForm.currentPassword, newPassword: passwordForm.newPassword }),
|
||||
});
|
||||
const data = await res.json().catch(() => null);
|
||||
if (!res.ok) throw new Error(data?.error?.message ?? 'No se pudo cambiar la contraseña');
|
||||
setPasswordForm({ currentPassword: '', newPassword: '', confirmPassword: '' });
|
||||
setPasswordMessage('Contraseña actualizada correctamente');
|
||||
} catch (error) {
|
||||
setPasswordError(error instanceof Error ? error.message : 'No se pudo cambiar la contraseña');
|
||||
} finally { setPasswordSaving(false); }
|
||||
};
|
||||
|
||||
const handleDelete = async (id: string) => {
|
||||
if (!user || !confirm('¿Eliminar esta dirección?')) return;
|
||||
try {
|
||||
await fetch(`${API}/users/${user.id}/addresses/${id}`, { method: 'DELETE', credentials: 'include' });
|
||||
await fetch(`/api/users/${user.id}/addresses/${id}`, { method: 'DELETE', credentials: 'include' });
|
||||
setMsg('Dirección eliminada');
|
||||
setTimeout(() => setMsg(''), 3000);
|
||||
load();
|
||||
@@ -247,6 +302,45 @@ export default function AccountPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Personal data */}
|
||||
<section className="rounded-xl border border-stone-200 bg-white p-5">
|
||||
<h2 className="mb-4 text-lg font-semibold text-stone-900">Datos personales</h2>
|
||||
<form onSubmit={saveProfile} className="grid gap-4 sm:grid-cols-2">
|
||||
<label className="text-sm font-medium text-stone-700">Nombre
|
||||
<input value={profileForm.displayName} onChange={(e) => setProfileForm(f => ({ ...f, displayName: e.target.value }))} required className="mt-1 w-full rounded-lg border border-stone-300 px-3 py-2 font-normal outline-none focus:ring-2 focus:ring-[#2D6A4F]" />
|
||||
</label>
|
||||
<label className="text-sm font-medium text-stone-700">Teléfono
|
||||
<input value={profileForm.phone} onChange={(e) => setProfileForm(f => ({ ...f, phone: e.target.value }))} className="mt-1 w-full rounded-lg border border-stone-300 px-3 py-2 font-normal outline-none focus:ring-2 focus:ring-[#2D6A4F]" />
|
||||
</label>
|
||||
{profileError && <p className="sm:col-span-2 text-sm text-red-600">{profileError}</p>}
|
||||
<button disabled={profileSaving} className="w-fit rounded-lg bg-[#2D6A4F] px-5 py-2 text-sm font-semibold text-white disabled:opacity-50">{profileSaving ? 'Guardando...' : 'Guardar datos'}</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
{/* Password */}
|
||||
<section className="rounded-xl border border-stone-200 bg-white p-5">
|
||||
<div className="mb-4 flex items-center justify-between gap-4">
|
||||
<h2 className="text-lg font-semibold text-stone-900">Contraseña</h2>
|
||||
<a href="/auth/recuperar" className="text-sm text-[#2D6A4F] hover:underline">¿Has olvidado tu contraseña?</a>
|
||||
</div>
|
||||
<form onSubmit={changePassword} className="max-w-xl space-y-4">
|
||||
<label className="block text-sm font-medium text-stone-700">Contraseña actual
|
||||
<input type="password" required value={passwordForm.currentPassword} onChange={(e) => setPasswordForm(f => ({ ...f, currentPassword: e.target.value }))} className="mt-1 w-full rounded-lg border border-stone-300 px-3 py-2 font-normal outline-none focus:ring-2 focus:ring-[#2D6A4F]" />
|
||||
</label>
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<label className="text-sm font-medium text-stone-700">Nueva contraseña
|
||||
<input type="password" minLength={8} required value={passwordForm.newPassword} onChange={(e) => setPasswordForm(f => ({ ...f, newPassword: e.target.value }))} className="mt-1 w-full rounded-lg border border-stone-300 px-3 py-2 font-normal outline-none focus:ring-2 focus:ring-[#2D6A4F]" />
|
||||
</label>
|
||||
<label className="text-sm font-medium text-stone-700">Repetir contraseña
|
||||
<input type="password" minLength={8} required value={passwordForm.confirmPassword} onChange={(e) => setPasswordForm(f => ({ ...f, confirmPassword: e.target.value }))} className="mt-1 w-full rounded-lg border border-stone-300 px-3 py-2 font-normal outline-none focus:ring-2 focus:ring-[#2D6A4F]" />
|
||||
</label>
|
||||
</div>
|
||||
{passwordError && <p className="text-sm text-red-600">{passwordError}</p>}
|
||||
{passwordMessage && <p className="text-sm text-green-700">{passwordMessage}</p>}
|
||||
<button disabled={passwordSaving} className="rounded-lg bg-[#2D6A4F] px-5 py-2 text-sm font-semibold text-white disabled:opacity-50">{passwordSaving ? 'Actualizando...' : 'Cambiar contraseña'}</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
{/* Addresses */}
|
||||
<section>
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
|
||||
17
project/frontend/src/app/api/auth/me/password/route.ts
Normal file
17
project/frontend/src/app/api/auth/me/password/route.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
|
||||
|
||||
export async function PATCH(request: NextRequest) {
|
||||
try {
|
||||
const response = await fetch(`${API}/auth/me/password`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json', Cookie: request.headers.get('cookie') ?? '' },
|
||||
body: await request.text(),
|
||||
});
|
||||
const data = await response.json().catch(() => ({}));
|
||||
return NextResponse.json(data, { status: response.status });
|
||||
} catch {
|
||||
return NextResponse.json({ error: { message: 'Error del servidor' } }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const backendRes = await fetch(`${API}/auth/password-reset/confirm`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const data = await backendRes.json().catch(() => ({ error: { message: 'Error del servidor' } }));
|
||||
return NextResponse.json(data, { status: backendRes.status });
|
||||
} catch {
|
||||
return NextResponse.json({ error: { message: 'Error del servidor' } }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const backendRes = await fetch(`${API}/auth/password-reset/request`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'x-forwarded-for': request.headers.get('x-forwarded-for') ?? '' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const data = await backendRes.json().catch(() => ({ error: { message: 'Error del servidor' } }));
|
||||
return NextResponse.json(data, { status: backendRes.status });
|
||||
} catch {
|
||||
return NextResponse.json({ error: { message: 'Error del servidor' } }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
|
||||
type Params = { params: Promise<{ id: string; addressId: string }> };
|
||||
|
||||
async function proxy(request: NextRequest, { params }: Params) {
|
||||
const { id, addressId } = await params;
|
||||
const response = await fetch(`${API}/users/${id}/addresses/${addressId}`, {
|
||||
method: request.method,
|
||||
headers: { Cookie: request.headers.get('cookie') ?? '' },
|
||||
cache: 'no-store',
|
||||
});
|
||||
if (response.status === 204) return new NextResponse(null, { status: 204 });
|
||||
const data = await response.json().catch(() => ({}));
|
||||
return NextResponse.json(data, { status: response.status });
|
||||
}
|
||||
|
||||
export async function PATCH(request: NextRequest, context: Params) { return proxy(request, context); }
|
||||
export async function DELETE(request: NextRequest, context: Params) { return proxy(request, context); }
|
||||
20
project/frontend/src/app/api/users/[id]/route.ts
Normal file
20
project/frontend/src/app/api/users/[id]/route.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
async function proxy(request: NextRequest, { params }: Params) {
|
||||
const { id } = await params;
|
||||
const body = request.method === 'PATCH' ? await request.text() : undefined;
|
||||
const response = await fetch(`${API}/users/${id}`, {
|
||||
method: request.method,
|
||||
headers: { Cookie: request.headers.get('cookie') ?? '', ...(body ? { 'Content-Type': 'application/json' } : {}) },
|
||||
...(body ? { body } : {}),
|
||||
cache: 'no-store',
|
||||
});
|
||||
const data = await response.json().catch(() => ({}));
|
||||
return NextResponse.json(data, { status: response.status });
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest, context: Params) { return proxy(request, context); }
|
||||
export async function PATCH(request: NextRequest, context: Params) { return proxy(request, context); }
|
||||
@@ -66,9 +66,10 @@ export default function LoginPage() {
|
||||
>
|
||||
{loading ? 'Entrando...' : 'Iniciar sesión'}
|
||||
</button>
|
||||
<p className="text-center text-sm text-gray-500">
|
||||
¿No tienes cuenta? <Link href="/auth/register" className="text-[#70ad47] hover:underline font-medium">Créala aquí</Link>
|
||||
</p>
|
||||
<div className="flex items-center justify-between text-sm text-gray-500">
|
||||
<Link href="/auth/recuperar" className="text-[#70ad47] hover:underline">¿Olvidaste tu contraseña?</Link>
|
||||
<span>¿No tienes cuenta? <Link href="/auth/register" className="text-[#70ad47] hover:underline font-medium">Créala aquí</Link></span>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
55
project/frontend/src/app/auth/recuperar/page.tsx
Normal file
55
project/frontend/src/app/auth/recuperar/page.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import Link from 'next/link';
|
||||
|
||||
export default function RecoverPage() {
|
||||
const [email, setEmail] = useState('');
|
||||
const [sent, setSent] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const submit = async (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
setError('');
|
||||
setLoading(true);
|
||||
try {
|
||||
const response = await fetch('/api/auth/password-reset/request', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email }),
|
||||
});
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => null);
|
||||
setError(data?.error?.message ?? 'No se pudo solicitar el enlace.');
|
||||
return;
|
||||
}
|
||||
setSent(true);
|
||||
} catch {
|
||||
setError('No se pudo conectar con el servidor.');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<main className="max-w-md mx-auto px-4 py-16">
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-8 shadow-sm">
|
||||
<h1 className="mb-2 text-2xl font-bold text-gray-900">Recuperar contraseña</h1>
|
||||
{sent ? (
|
||||
<p className="rounded-lg bg-green-50 px-4 py-3 text-sm text-green-700">Si existe una cuenta con ese email, recibirás un enlace de recuperación.</p>
|
||||
) : (
|
||||
<form onSubmit={submit} className="space-y-4">
|
||||
<p className="text-sm text-gray-500">Te enviaremos un enlace para elegir una contraseña nueva.</p>
|
||||
<label className="block text-sm font-medium text-gray-700">Email
|
||||
<input type="email" required value={email} onChange={(e) => setEmail(e.target.value)} className="mt-1 w-full rounded-xl border border-gray-300 px-4 py-3 outline-none focus:ring-2 focus:ring-[#70ad47]" />
|
||||
</label>
|
||||
{error && <p className="rounded-lg bg-red-50 px-4 py-3 text-sm text-red-700">{error}</p>}
|
||||
<button disabled={loading} className="w-full rounded-xl bg-[#70ad47] py-3 font-semibold text-white hover:bg-[#5a9040] disabled:opacity-60">{loading ? 'Enviando...' : 'Enviar enlace'}</button>
|
||||
</form>
|
||||
)}
|
||||
<Link href="/auth/login" className="mt-5 block text-center text-sm text-[#70ad47] hover:underline">Volver a iniciar sesión</Link>
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
73
project/frontend/src/app/cuenta/restablecer/page.tsx
Normal file
73
project/frontend/src/app/cuenta/restablecer/page.tsx
Normal file
@@ -0,0 +1,73 @@
|
||||
'use client';
|
||||
|
||||
import { Suspense, useState } from 'react';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
|
||||
function ResetForm() {
|
||||
const router = useRouter();
|
||||
const params = useSearchParams();
|
||||
const token = params.get('token') ?? '';
|
||||
const [password, setPassword] = useState('');
|
||||
const [confirm, setConfirm] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [done, setDone] = useState(false);
|
||||
|
||||
if (!token) return <Message>El enlace de recuperación no es válido.</Message>;
|
||||
if (done) return <Message success>Contraseña actualizada. Redirigiendo al inicio de sesión...</Message>;
|
||||
|
||||
const submit = async (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
setError('');
|
||||
if (password.length < 8) return setError('La contraseña debe tener al menos 8 caracteres.');
|
||||
if (password !== confirm) return setError('Las contraseñas no coinciden.');
|
||||
setSaving(true);
|
||||
try {
|
||||
const response = await fetch('/api/auth/password-reset/confirm', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ token, password }),
|
||||
});
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
setError(data?.error?.message ?? data?.message ?? 'El enlace es inválido o ha caducado.');
|
||||
return;
|
||||
}
|
||||
setDone(true);
|
||||
window.setTimeout(() => router.push('/auth/login'), 2200);
|
||||
} catch {
|
||||
setError('No se pudo conectar con el servidor. Inténtalo de nuevo.');
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={submit} className="w-full max-w-md space-y-5 rounded-2xl border border-gray-200 bg-white p-8 shadow-sm">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-gray-900">Restablecer contraseña</h1>
|
||||
<p className="mt-1 text-sm text-gray-500">Elige una contraseña nueva para tu cuenta.</p>
|
||||
</div>
|
||||
<label className="block text-sm font-medium text-gray-700">
|
||||
Nueva contraseña
|
||||
<input type="password" minLength={8} required value={password} onChange={(e) => setPassword(e.target.value)} className="mt-1 w-full rounded-xl border border-gray-300 px-4 py-3 outline-none focus:ring-2 focus:ring-[#70ad47]" />
|
||||
</label>
|
||||
<label className="block text-sm font-medium text-gray-700">
|
||||
Repetir contraseña
|
||||
<input type="password" minLength={8} required value={confirm} onChange={(e) => setConfirm(e.target.value)} className="mt-1 w-full rounded-xl border border-gray-300 px-4 py-3 outline-none focus:ring-2 focus:ring-[#70ad47]" />
|
||||
</label>
|
||||
{error && <p className="rounded-lg bg-red-50 px-4 py-3 text-sm text-red-700">{error}</p>}
|
||||
<button disabled={saving} className="w-full rounded-xl bg-[#70ad47] py-3 font-semibold text-white hover:bg-[#5a9040] disabled:opacity-60">
|
||||
{saving ? 'Guardando...' : 'Cambiar contraseña'}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
function Message({ children, success = false }: { children: React.ReactNode; success?: boolean }) {
|
||||
return <div className={`w-full max-w-md rounded-2xl border p-8 text-sm ${success ? 'border-green-200 bg-green-50 text-green-700' : 'border-gray-200 bg-white text-gray-700'}`}>{children}</div>;
|
||||
}
|
||||
|
||||
export default function ResetPasswordPage() {
|
||||
return <main className="flex min-h-[70vh] items-center justify-center bg-gray-50 px-4 py-12"><Suspense fallback={<Message>Cargando...</Message>}><ResetForm /></Suspense></main>;
|
||||
}
|
||||
@@ -11,28 +11,38 @@ export default function UserMenu() {
|
||||
<span className="text-sm text-gray-600 hidden sm:block">
|
||||
{user.email}
|
||||
</span>
|
||||
<button
|
||||
onClick={logout}
|
||||
className="flex items-center gap-1.5 text-sm font-medium text-[#70ad47] hover:text-[#5a9040] transition-colors"
|
||||
>
|
||||
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} aria-label="Cerrar sesión">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75" />
|
||||
</svg>
|
||||
Cerrar sesión
|
||||
</button>
|
||||
<div className="relative group">
|
||||
<button
|
||||
onClick={logout}
|
||||
aria-label="Cerrar sesión"
|
||||
className="flex items-center justify-center w-9 h-9 rounded-full text-[#70ad47] hover:bg-[#70ad47]/10 hover:text-[#5a9040] transition-colors"
|
||||
>
|
||||
<svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} aria-hidden="true">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75" />
|
||||
</svg>
|
||||
</button>
|
||||
<span role="tooltip" className="pointer-events-none absolute right-0 top-full z-10 mt-2 whitespace-nowrap rounded-md bg-gray-900 px-2 py-1 text-xs text-white opacity-0 shadow transition-opacity group-hover:opacity-100 group-focus-within:opacity-100">
|
||||
Cerrar sesión
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Link
|
||||
href="/auth/login"
|
||||
className="flex items-center gap-1.5 text-sm font-medium text-[#70ad47] hover:text-[#5a9040] transition-colors"
|
||||
>
|
||||
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} aria-label="Iniciar sesión">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" d="M15.75 6a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0zM4.501 20.118a7.5 7.5 0 0114.998 0A17.933 17.933 0 0112 21.75c-2.676 0-5.216-.584-7.499-1.632z" />
|
||||
</svg>
|
||||
Iniciar sesión
|
||||
</Link>
|
||||
<div className="relative group">
|
||||
<Link
|
||||
href="/auth/login"
|
||||
aria-label="Iniciar sesión"
|
||||
className="flex items-center justify-center w-9 h-9 rounded-full text-[#70ad47] hover:bg-[#70ad47]/10 hover:text-[#5a9040] transition-colors"
|
||||
>
|
||||
<svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} aria-hidden="true">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" d="M15.75 6a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0zM4.501 20.118a7.5 7.5 0 0114.998 0A17.933 17.933 0 0112 21.75c-2.676 0-5.216-.584-7.499-1.632z" />
|
||||
</svg>
|
||||
</Link>
|
||||
<span role="tooltip" className="pointer-events-none absolute right-0 top-full z-10 mt-2 whitespace-nowrap rounded-md bg-gray-900 px-2 py-1 text-xs text-white opacity-0 shadow transition-opacity group-hover:opacity-100 group-focus-within:opacity-100">
|
||||
Iniciar sesión
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
21
project/package-lock.json
generated
21
project/package-lock.json
generated
@@ -15,6 +15,7 @@
|
||||
"argon2": "^0.45.1",
|
||||
"fastify": "^5.2.0",
|
||||
"node-pg-migrate": "^9.0.0",
|
||||
"nodemailer": "^9.0.5",
|
||||
"pg": "^8.23.0",
|
||||
"pino": "^10.3.1",
|
||||
"stripe": "^22.5.0",
|
||||
@@ -22,6 +23,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.17.0",
|
||||
"@types/nodemailer": "^8.0.1",
|
||||
"@types/pg": "^8.21.0",
|
||||
"eslint": "^9.17.0",
|
||||
"eslint-config-prettier": "^10.0.0",
|
||||
@@ -1445,6 +1447,16 @@
|
||||
"undici-types": "~8.3.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/nodemailer": {
|
||||
"version": "8.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-8.0.1.tgz",
|
||||
"integrity": "sha512-PxpaInm8V1JQDd4j0ds5HfvWQk8JupS1C0Picb96QJsrrRDjBH+DlK7L4ZdNSqNULhiZRQHc40nLVShaGxXAMw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/pg": {
|
||||
"version": "8.21.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.21.0.tgz",
|
||||
@@ -3364,6 +3376,15 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/nodemailer": {
|
||||
"version": "9.0.5",
|
||||
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-9.0.5.tgz",
|
||||
"integrity": "sha512-wvjiKvjczmsN7U/8006JOdXubgBk2XFAbioDMbT+sM7cPs0QrhJTa6KBRX7P5REGGkDcLUz/EarWidb8G8C1jQ==",
|
||||
"license": "MIT-0",
|
||||
"engines": {
|
||||
"node": ">=6.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/on-exit-leak-free": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/on-exit-leak-free/-/on-exit-leak-free-2.1.2.tgz",
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
"argon2": "^0.45.1",
|
||||
"fastify": "^5.2.0",
|
||||
"node-pg-migrate": "^9.0.0",
|
||||
"nodemailer": "^9.0.5",
|
||||
"pg": "^8.23.0",
|
||||
"pino": "^10.3.1",
|
||||
"stripe": "^22.5.0",
|
||||
@@ -37,6 +38,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.17.0",
|
||||
"@types/nodemailer": "^8.0.1",
|
||||
"@types/pg": "^8.21.0",
|
||||
"eslint": "^9.17.0",
|
||||
"eslint-config-prettier": "^10.0.0",
|
||||
|
||||
@@ -45,6 +45,7 @@ import { LoggingEmailProvider } from '../modules/notifications/index.js';
|
||||
import { createFlagStore, type FeatureFlagProvider } from '../modules/flags/index.js';
|
||||
import { AppError, errorEnvelope } from '../shared/errors.js';
|
||||
import { createLogger, type Logger } from '../infrastructure/logging/logger.js';
|
||||
import { SettingsPasswordResetMailer } from '../modules/identity/infrastructure/settings-password-reset-mailer.js';
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
@@ -185,6 +186,7 @@ export async function buildApp(deps: BuildAppDeps = {}): Promise<FastifyInstance
|
||||
cookieSecure: deps.cookieSecure,
|
||||
authenticate,
|
||||
passwordReset: {
|
||||
mailer: new SettingsPasswordResetMailer(deps.pool as pg.Pool),
|
||||
audit: (entry) => {
|
||||
// Best-effort audit; never block the request on audit failures.
|
||||
void auditLogger
|
||||
|
||||
@@ -266,7 +266,7 @@ export async function registerCatalogRoutes(
|
||||
|
||||
const settingsResult = await deps.pool.query<{ key: string; value: string }>(
|
||||
`SELECT key, value FROM store_settings WHERE key = ANY($1::text[])`,
|
||||
[['ai_base_url', 'ai_model', 'ai_api_key', 'ai_seo_title_prompt', 'ai_seo_description_prompt']],
|
||||
[['ai_base_url', 'ai_model', 'ai_api_key', 'ai_seo_title_prompt', 'ai_seo_description_prompt', 'ai_product_description_prompt']],
|
||||
);
|
||||
const settings = Object.fromEntries(settingsResult.rows.map((row) => [row.key, row.value]));
|
||||
const baseUrl = settings.ai_base_url?.trim();
|
||||
@@ -283,7 +283,10 @@ export async function registerCatalogRoutes(
|
||||
};
|
||||
const promptFor = (template: string | undefined, fallback: string) =>
|
||||
(template || fallback).replace(/\{\{(name|description|brand)\}\}/g, (_, key: string) => replacements[key] ?? '');
|
||||
const patch: { seoTitle?: string; seoDescription?: string } = {};
|
||||
const patch: { description?: string; seoTitle?: string; seoDescription?: string } = {};
|
||||
if (!product.description?.trim()) {
|
||||
patch.description = (await generateWithModel(baseUrl, model, apiKey, promptFor(settings.ai_product_description_prompt, 'Escribe una descripción comercial clara y útil en español para este producto: {{name}}. Devuelve solo la descripción.'))).slice(0, 2_000);
|
||||
}
|
||||
if (!product.seoTitle?.trim()) {
|
||||
patch.seoTitle = (await generateWithModel(baseUrl, model, apiKey, promptFor(settings.ai_seo_title_prompt, 'Genera un título SEO breve para {{name}}. Devuelve solo el título.'))).slice(0, 200);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import {
|
||||
RequestPasswordReset,
|
||||
} from '../application/password-reset.js';
|
||||
import { InvalidResetTokenError } from '../domain/password-reset.js';
|
||||
import { createPasswordResetMailer } from '../infrastructure/smtp-password-reset-mailer.js';
|
||||
|
||||
export const SESSION_COOKIE_NAME = 'mdv_session';
|
||||
|
||||
@@ -234,15 +235,30 @@ export async function registerIdentityRoutes(
|
||||
}
|
||||
});
|
||||
|
||||
app.patch('/auth/me/password', async (request, reply) => {
|
||||
const user = await deps.authenticate!(request);
|
||||
const input = parseJson(
|
||||
z.object({ currentPassword: z.string().min(1).max(128), newPassword: z.string().min(8).max(128) }),
|
||||
request.body,
|
||||
);
|
||||
const record = await users.findByEmail(user.email);
|
||||
if (!record || !(await hasher.verify(record.passwordHash, input.currentPassword))) {
|
||||
throw new AppError(400, 'INVALID_CURRENT_PASSWORD', 'La contraseña actual no es válida');
|
||||
}
|
||||
await users.updateUser(user.id, { passwordHash: await hasher.hash(input.newPassword) });
|
||||
return reply.send({ ok: true });
|
||||
});
|
||||
|
||||
if (deps.passwordReset) {
|
||||
const pr = deps.passwordReset;
|
||||
const tokens = pr.tokens ?? new PgPasswordResetTokenRepository(deps.pool);
|
||||
const usersRepo = new PgUserRepository(deps.pool);
|
||||
const rateLimiter = pr.rateLimiter ?? new InMemoryResetRateLimiter();
|
||||
const mailer = pr.mailer ?? new LoggingPasswordResetMailer();
|
||||
const mailer = pr.mailer ?? createPasswordResetMailer();
|
||||
const publicAppUrl = (process.env.PUBLIC_APP_URL ?? 'https://mercadodevida.es').replace(/\/$/, '');
|
||||
const buildResetUrl =
|
||||
pr.buildResetUrl ??
|
||||
((token: string) => `/cuenta/restablecer?token=${encodeURIComponent(token)}`);
|
||||
((token: string) => `${publicAppUrl}/cuenta/restablecer?token=${encodeURIComponent(token)}`);
|
||||
|
||||
const requestReset = new RequestPasswordReset({
|
||||
users: usersRepo,
|
||||
@@ -293,6 +309,15 @@ export async function registerIdentityRoutes(
|
||||
'/auth/password-reset/request',
|
||||
{ schema: requestSchema },
|
||||
async (request, reply) => {
|
||||
if (mailer.assertReady) {
|
||||
try {
|
||||
await mailer.assertReady();
|
||||
} catch {
|
||||
throw new AppError(422, 'EMAIL_DELIVERY_NOT_CONFIGURED', 'Configura SMTP en Ajustes → SMTP / Email');
|
||||
}
|
||||
} else if (mailer.isConfigured && !mailer.isConfigured()) {
|
||||
throw new AppError(422, 'EMAIL_DELIVERY_NOT_CONFIGURED', 'Configura SMTP en Ajustes → SMTP / Email');
|
||||
}
|
||||
const input = parseJson(
|
||||
z.object({ email: z.email().max(255) }),
|
||||
request.body,
|
||||
@@ -350,22 +375,6 @@ export class InMemoryResetRateLimiter implements ResetRateLimiter {
|
||||
}
|
||||
}
|
||||
|
||||
/** Logs the reset email to stdout; production should replace with a real provider. */
|
||||
export class LoggingPasswordResetMailer implements PasswordResetMailer {
|
||||
async sendPasswordReset(input: { email: string; resetUrl: string; locale?: string }): Promise<void> {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
level: 'info',
|
||||
msg: 'password_reset_email',
|
||||
to: input.email,
|
||||
url: input.resetUrl,
|
||||
locale: input.locale ?? 'es',
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function setSessionCookie(reply: FastifyReply, token: string, secure: boolean): void {
|
||||
void reply.setCookie(SESSION_COOKIE_NAME, token, {
|
||||
path: '/',
|
||||
|
||||
@@ -50,5 +50,8 @@ export interface ResetRateLimiter {
|
||||
}
|
||||
|
||||
export interface PasswordResetMailer {
|
||||
/** Optional readiness check; absent in test doubles and legacy adapters. */
|
||||
isConfigured?: () => boolean;
|
||||
assertReady?: () => Promise<void>;
|
||||
sendPasswordReset(input: { email: string; resetUrl: string; locale?: string }): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import type pg from 'pg';
|
||||
import type { PasswordResetMailer } from '../domain/ports.js';
|
||||
import { SmtpPasswordResetMailer, type SmtpPasswordResetMailerOptions } from './smtp-password-reset-mailer.js';
|
||||
|
||||
const SMTP_KEYS = ['smtp_host', 'smtp_port', 'smtp_secure', 'smtp_user', 'smtp_pass', 'smtp_from'] as const;
|
||||
|
||||
/** Reads SMTP credentials from store_settings so admins can change them without a deploy. */
|
||||
export class SettingsPasswordResetMailer implements PasswordResetMailer {
|
||||
constructor(private readonly pool: pg.Pool) {}
|
||||
|
||||
isConfigured(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
async assertReady(): Promise<void> {
|
||||
await this.readMailer();
|
||||
}
|
||||
|
||||
async sendPasswordReset(input: { email: string; resetUrl: string; locale?: string }): Promise<void> {
|
||||
const mailer = await this.readMailer();
|
||||
await mailer.sendPasswordReset(input);
|
||||
}
|
||||
|
||||
private async readMailer(): Promise<SmtpPasswordResetMailer> {
|
||||
const result = await this.pool.query<{ key: string; value: string }>(
|
||||
`SELECT key, value FROM store_settings WHERE key = ANY($1::text[])`,
|
||||
[SMTP_KEYS],
|
||||
);
|
||||
const settings = Object.fromEntries(result.rows.map((row) => [row.key, row.value]));
|
||||
const host = settings.smtp_host?.trim();
|
||||
const user = settings.smtp_user?.trim();
|
||||
const password = settings.smtp_pass;
|
||||
const from = settings.smtp_from?.trim() || user;
|
||||
if (!host || !user || !password || !from) {
|
||||
throw new Error('SMTP is not configured in Ajustes → SMTP / Email');
|
||||
}
|
||||
const port = Number(settings.smtp_port || '465');
|
||||
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
||||
throw new Error('SMTP port is invalid in Ajustes → SMTP / Email');
|
||||
}
|
||||
const options: SmtpPasswordResetMailerOptions = {
|
||||
host,
|
||||
port,
|
||||
secure: settings.smtp_secure !== 'false' || port === 465,
|
||||
user,
|
||||
password,
|
||||
from,
|
||||
};
|
||||
return new SmtpPasswordResetMailer(options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import nodemailer, { type Transporter } from 'nodemailer';
|
||||
import type { PasswordResetMailer } from '../domain/ports.js';
|
||||
|
||||
export interface SmtpPasswordResetMailerOptions {
|
||||
host: string;
|
||||
port: number;
|
||||
secure: boolean;
|
||||
user: string;
|
||||
password: string;
|
||||
from: string;
|
||||
}
|
||||
|
||||
/** Sends password-reset messages through an authenticated SMTP server. */
|
||||
export class SmtpPasswordResetMailer implements PasswordResetMailer {
|
||||
private readonly transporter: Transporter;
|
||||
|
||||
constructor(private readonly options: SmtpPasswordResetMailerOptions) {
|
||||
this.transporter = nodemailer.createTransport({
|
||||
host: options.host,
|
||||
port: options.port,
|
||||
secure: options.secure,
|
||||
auth: { user: options.user, pass: options.password },
|
||||
});
|
||||
}
|
||||
|
||||
isConfigured(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
async assertReady(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async sendPasswordReset(input: { email: string; resetUrl: string; locale?: string }): Promise<void> {
|
||||
await this.transporter.sendMail({
|
||||
from: this.options.from,
|
||||
to: input.email,
|
||||
subject: 'Restablece tu contraseña — Mercado de Vida',
|
||||
text: [
|
||||
'Has solicitado restablecer tu contraseña de Mercado de Vida.',
|
||||
'',
|
||||
`Abre este enlace para continuar: ${input.resetUrl}`,
|
||||
'',
|
||||
'Si no solicitaste este cambio, puedes ignorar este correo.',
|
||||
'El enlace caduca en una hora y solo puede utilizarse una vez.',
|
||||
].join('\n'),
|
||||
html: [
|
||||
'<p>Has solicitado restablecer tu contraseña de Mercado de Vida.</p>',
|
||||
`<p><a href="${escapeHtml(input.resetUrl)}">Restablecer contraseña</a></p>`,
|
||||
'<p>Si no solicitaste este cambio, puedes ignorar este correo.</p>',
|
||||
'<p>El enlace caduca en una hora y solo puede utilizarse una vez.</p>',
|
||||
].join(''),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function createPasswordResetMailer(env: NodeJS.ProcessEnv = process.env): PasswordResetMailer {
|
||||
const host = env.SMTP_HOST?.trim();
|
||||
const user = env.SMTP_USER?.trim();
|
||||
const password = env.SMTP_PASS;
|
||||
const from = env.SMTP_FROM?.trim() || user;
|
||||
if (!host || !user || !password || !from) {
|
||||
return {
|
||||
isConfigured: () => false,
|
||||
async assertReady() {
|
||||
throw new Error('SMTP is not configured; set SMTP_HOST, SMTP_USER, SMTP_PASS and SMTP_FROM');
|
||||
},
|
||||
async sendPasswordReset() {
|
||||
throw new Error('SMTP is not configured; set SMTP_HOST, SMTP_USER, SMTP_PASS and SMTP_FROM');
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const port = Number(env.SMTP_PORT ?? '587');
|
||||
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
||||
throw new Error('SMTP_PORT must be a valid TCP port');
|
||||
}
|
||||
const secure = env.SMTP_SECURE === 'true' || port === 465;
|
||||
return new SmtpPasswordResetMailer({ host, port, secure, user, password, from });
|
||||
}
|
||||
|
||||
function escapeHtml(value: string): string {
|
||||
return value.replace(/[&<>"']/g, (character) => ({
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": ''',
|
||||
})[character] ?? character);
|
||||
}
|
||||
@@ -27,6 +27,13 @@ const updateSettingsSchema = z.object({
|
||||
aiApiKey: z.string().max(500).optional(),
|
||||
aiSeoTitlePrompt: z.string().max(2000).optional(),
|
||||
aiSeoDescriptionPrompt: z.string().max(4000).optional(),
|
||||
aiProductDescriptionPrompt: z.string().max(4000).optional(),
|
||||
smtpHost: z.string().max(255).optional(),
|
||||
smtpPort: z.coerce.number().int().min(1).max(65535).optional(),
|
||||
smtpSecure: z.boolean().optional(),
|
||||
smtpUser: z.string().max(255).optional(),
|
||||
smtpPass: z.string().max(500).optional(),
|
||||
smtpFrom: z.string().email().optional().or(z.literal('')),
|
||||
});
|
||||
|
||||
const SETTING_KEYS: Record<string, string> = {
|
||||
@@ -44,6 +51,13 @@ const SETTING_KEYS: Record<string, string> = {
|
||||
aiApiKey: 'ai_api_key',
|
||||
aiSeoTitlePrompt: 'ai_seo_title_prompt',
|
||||
aiSeoDescriptionPrompt: 'ai_seo_description_prompt',
|
||||
aiProductDescriptionPrompt: 'ai_product_description_prompt',
|
||||
smtpHost: 'smtp_host',
|
||||
smtpPort: 'smtp_port',
|
||||
smtpSecure: 'smtp_secure',
|
||||
smtpUser: 'smtp_user',
|
||||
smtpPass: 'smtp_pass',
|
||||
smtpFrom: 'smtp_from',
|
||||
};
|
||||
|
||||
export async function registerStoreSettingsRoutes(
|
||||
@@ -82,6 +96,14 @@ export async function registerStoreSettingsRoutes(
|
||||
aiApiKeyConfigured: Boolean(map['ai_api_key']),
|
||||
aiSeoTitlePrompt: map['ai_seo_title_prompt'] ?? 'Genera un título SEO breve y atractivo para este producto: {{name}}. Devuelve solo el título.',
|
||||
aiSeoDescriptionPrompt: map['ai_seo_description_prompt'] ?? 'Genera una meta descripción SEO en español, clara y persuasiva, para este producto: {{name}}. Devuelve solo la descripción.',
|
||||
aiProductDescriptionPrompt: map['ai_product_description_prompt'] ?? 'Escribe una descripción comercial clara y útil en español para este producto: {{name}}. Incluye sus beneficios y características usando solo la información disponible. Devuelve solo la descripción.',
|
||||
smtpHost: map['smtp_host'] ?? process.env.SMTP_HOST ?? '',
|
||||
smtpPort: map['smtp_port'] ?? process.env.SMTP_PORT ?? '465',
|
||||
smtpSecure: (map['smtp_secure'] ?? process.env.SMTP_SECURE ?? 'true') !== 'false',
|
||||
smtpUser: map['smtp_user'] ?? process.env.SMTP_USER ?? '',
|
||||
smtpPass: '',
|
||||
smtpPassConfigured: Boolean(map['smtp_pass'] || process.env.SMTP_PASS),
|
||||
smtpFrom: map['smtp_from'] ?? process.env.SMTP_FROM ?? '',
|
||||
});
|
||||
});
|
||||
|
||||
@@ -150,7 +172,15 @@ export async function registerStoreSettingsRoutes(
|
||||
aiApiKey: '',
|
||||
aiApiKeyConfigured: Boolean(map['ai_api_key']),
|
||||
aiSeoTitlePrompt: map['ai_seo_title_prompt'] ?? 'Genera un título SEO breve y atractivo para este producto: {{name}}. Devuelve solo el título.',
|
||||
aiSeoDescriptionPrompt: map['ai_seo_description_prompt'] ?? 'Genera una meta descripción SEO en español, clara y persuasiva, para este producto: {{name}}. Devuelve solo la descripción.',
|
||||
aiSeoDescriptionPrompt: map['ai_seo_description_prompt'] ?? 'Genera una meta descripción SEO en español, clara y persuasiva, para este producto: {{name}}. Incluye sus beneficios y características usando solo la información disponible. Devuelve solo la descripción.',
|
||||
aiProductDescriptionPrompt: map['ai_product_description_prompt'] ?? 'Escribe una descripción comercial clara y útil en español para este producto: {{name}}. Incluye sus beneficios y características usando solo la información disponible. Devuelve solo la descripción.',
|
||||
smtpHost: map['smtp_host'] ?? process.env.SMTP_HOST ?? '',
|
||||
smtpPort: map['smtp_port'] ?? process.env.SMTP_PORT ?? '465',
|
||||
smtpSecure: (map['smtp_secure'] ?? process.env.SMTP_SECURE ?? 'true') !== 'false',
|
||||
smtpUser: map['smtp_user'] ?? process.env.SMTP_USER ?? '',
|
||||
smtpPass: '',
|
||||
smtpPassConfigured: Boolean(map['smtp_pass'] || process.env.SMTP_PASS),
|
||||
smtpFrom: map['smtp_from'] ?? process.env.SMTP_FROM ?? '',
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 8.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 950 B |
Binary file not shown.
|
After Width: | Height: | Size: 146 KiB |
Reference in New Issue
Block a user