import type { APIRoute } from 'astro'; const POCKETBASE_URL = import.meta.env.POCKETBASE_URL || 'http://127.0.0.1:8090'; export const POST: APIRoute = async ({ request }) => { try { const data = await request.json(); const { token, userId } = data; if (!token || !userId) { return new Response(JSON.stringify({ success: false, error: 'Отсутствуют параметры' }), { status: 400 }); } const decoded = Buffer.from(token, 'base64').toString('utf8'); const parts = decoded.split(':'); if (parts.length < 3) { return new Response(JSON.stringify({ success: false, error: 'Неверный формат токена' }), { status: 400 }); } const [tokenUserId, email, timestamp] = parts; if (tokenUserId !== userId) { return new Response(JSON.stringify({ success: false, error: 'Неверный токен' }), { status: 400 }); } const tokenTime = parseInt(timestamp); const now = Date.now(); const maxAge = 24 * 60 * 60 * 1000; if (now - tokenTime > maxAge) { return new Response(JSON.stringify({ success: false, error: 'Срок действия ссылки истёк' }), { status: 400 }); } const response = await fetch(`${POCKETBASE_URL}/api/collections/users/records/${userId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ verified: true }), }); if (!response.ok) { const err = await response.json(); console.error('Verify error:', err); return new Response(JSON.stringify({ success: false, error: 'Не удалось подтвердить email' }), { status: 400 }); } return new Response(JSON.stringify({ success: true, message: 'Email подтверждён' }), { status: 200 }); } catch (error: any) { console.error('Confirm error:', error); return new Response(JSON.stringify({ success: false, error: 'Ошибка при подтверждении' }), { status: 400 }); } };