2026-04-15 18:08:26 +05:00
|
|
|
|
import type { APIRoute } from 'astro';
|
2026-04-27 21:29:12 +05:00
|
|
|
|
|
|
|
|
|
|
const POCKETBASE_URL = import.meta.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
|
2026-04-15 18:08:26 +05:00
|
|
|
|
|
|
|
|
|
|
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 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-27 21:29:12 +05:00
|
|
|
|
const response = await fetch(`${POCKETBASE_URL}/api/collections/users/records/${userId}`, {
|
2026-04-15 18:08:26 +05:00
|
|
|
|
method: 'PATCH',
|
2026-04-27 21:29:12 +05:00
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2026-04-15 18:08:26 +05:00
|
|
|
|
body: JSON.stringify({ verified: true }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-04-27 21:29:12 +05:00
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
const err = await response.json();
|
|
|
|
|
|
console.error('Verify error:', err);
|
2026-04-15 18:08:26 +05:00
|
|
|
|
return new Response(JSON.stringify({
|
|
|
|
|
|
success: false,
|
2026-04-27 21:29:12 +05:00
|
|
|
|
error: 'Не удалось подтвердить email'
|
2026-04-15 18:08:26 +05:00
|
|
|
|
}), { 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 });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|