init: базовая структура АТ
This commit is contained in:
74
utils/mailTmApi.ts
Normal file
74
utils/mailTmApi.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
// 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(token: string, timeout = 60000) {
|
||||
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 message of messages) {
|
||||
console.log('🔖 Тема письма:', message.subject);
|
||||
|
||||
const msg = await axios.get(`${MAIL_TM_BASE}/messages/${message.id}`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
|
||||
const body = msg.data.text || msg.data.html;
|
||||
console.log('📩 Тело письма:', body);
|
||||
|
||||
const match = body.match(/\b\d{6}\b/);
|
||||
if (match) {
|
||||
console.log('✅ Код подтверждения найден:', match[0]);
|
||||
return match[0];
|
||||
}
|
||||
}
|
||||
|
||||
await new Promise(res => setTimeout(res, 2000)); // Пауза 2 сек
|
||||
}
|
||||
|
||||
throw new Error('Код подтверждения не получен');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user