91 lines
2.4 KiB
TypeScript
91 lines
2.4 KiB
TypeScript
// utils/mailTmApi.ts
|
||
|
||
import axios from 'axios';
|
||
|
||
const MAIL_TM_BASE = 'https://api.mail.tm';
|
||
|
||
export async function createTempEmail() {
|
||
const timestamp = Date.now();
|
||
|
||
// Получаем доступный домен
|
||
const domainRes = await axios.get(`${MAIL_TM_BASE}/domains`);
|
||
const domain = domainRes.data['hydra:member'][0].domain;
|
||
|
||
const email = `testuser${timestamp}@${domain}`;
|
||
const password = 'QwEr!1234567';
|
||
|
||
// Регистрируем
|
||
await axios.post(`${MAIL_TM_BASE}/accounts`, {
|
||
address: email,
|
||
password,
|
||
});
|
||
|
||
// Авторизуемся
|
||
const tokenRes = await axios.post(`${MAIL_TM_BASE}/token`, {
|
||
address: email,
|
||
password,
|
||
});
|
||
|
||
const token = tokenRes.data.token;
|
||
|
||
return { email, password, token };
|
||
}
|
||
|
||
|
||
export async function waitForConfirmationCode(email: string, token: string, type: 'register' | 'recover', timeout = 60000) {
|
||
console.log('📧 Ожидаем код для:', email);
|
||
const start = Date.now();
|
||
|
||
while (Date.now() - start < timeout) {
|
||
const res = await axios.get(`${MAIL_TM_BASE}/messages`, {
|
||
headers: { Authorization: `Bearer ${token}` },
|
||
});
|
||
|
||
const messages = res.data['hydra:member'];
|
||
|
||
if (messages.length === 0) {
|
||
console.log('⏳ Письма пока не пришли...');
|
||
} else {
|
||
console.log(`📬 Получено ${messages.length} писем:`);
|
||
for (const m of messages) {
|
||
console.log(`🧾 Письмо: id=${m.id}, тема="${m.subject}"`);
|
||
}
|
||
}
|
||
|
||
for (const message of messages) {
|
||
console.log('🔖 Тема письма:', message.subject);
|
||
|
||
// 🔴 ФИЛЬТРАЦИЯ ЗДЕСЬ
|
||
if (
|
||
type === 'register' &&
|
||
!message.subject.toLowerCase().includes('подтверж')
|
||
) {
|
||
continue;
|
||
}
|
||
|
||
if (
|
||
type === 'recover' &&
|
||
!message.subject.toLowerCase().includes('сброс')
|
||
) {
|
||
continue;
|
||
}
|
||
|
||
// ⬇️ ТОЛЬКО НУЖНОЕ ПИСЬМО ДОХОДИТ СЮДА
|
||
const msg = await axios.get(`${MAIL_TM_BASE}/messages/${message.id}`, {
|
||
headers: { Authorization: `Bearer ${token}` },
|
||
});
|
||
|
||
const body = msg.data.text || msg.data.html;
|
||
const match = body.match(/\b\d{6}\b/);
|
||
|
||
if (match) {
|
||
console.log('✅ Код найден:', match[0]);
|
||
return match[0];
|
||
}
|
||
}
|
||
|
||
await new Promise(res => setTimeout(res, 2000));
|
||
}
|
||
|
||
throw new Error('Код подтверждения не получен');
|
||
} |