Tighten Telegram bot login UX
All checks were successful
Build and deploy Backend / build (push) Successful in 36s

This commit is contained in:
Ruslan Bakiev
2026-05-08 19:52:07 +07:00
parent de0e230632
commit 387a504801

View File

@@ -110,13 +110,25 @@ export async function fetchTelegramPhoto(fileId: string) {
}; };
} }
async function sendLoginMessage(chatId: number, text: string, token?: string) { function formatRemaining(expiresAt: Date) {
const seconds = Math.max(0, Math.ceil((expiresAt.getTime() - Date.now()) / 1000));
const minutes = Math.floor(seconds / 60).toString();
const rest = (seconds % 60).toString().padStart(2, '0');
return `${minutes}:${rest}`;
}
async function sendLoginMessage(
chatId: number,
text: string,
token?: string,
expiresAt?: Date,
) {
const replyMarkup = token const replyMarkup = token
? { ? {
inline_keyboard: [ inline_keyboard: [
[ [
{ {
text: 'Вернуться в MapFlow', text: `Открыть MapFlow · ${formatRemaining(expiresAt ?? expiresIn(0))}`,
url: `${config.webAppUrl}?telegram_login=${encodeURIComponent(token)}`, url: `${config.webAppUrl}?telegram_login=${encodeURIComponent(token)}`,
}, },
], ],
@@ -159,7 +171,7 @@ export async function getTelegramBotLoginStatus(token: string) {
return { status: 'EXPIRED', sessionToken: null, user: null }; return { status: 'EXPIRED', sessionToken: null, user: null };
} }
if (request.status === 'PENDING' && request.expiresAt <= new Date()) { if (request.expiresAt <= new Date()) {
await prisma.telegramLoginRequest.update({ await prisma.telegramLoginRequest.update({
where: { id: request.id }, where: { id: request.id },
data: { status: 'EXPIRED' }, data: { status: 'EXPIRED' },
@@ -202,7 +214,7 @@ export async function handleTelegramBotWebhook(
}); });
if (!request || request.status !== 'PENDING' || request.expiresAt <= new Date()) { if (!request || request.status !== 'PENDING' || request.expiresAt <= new Date()) {
await sendLoginMessage(chatId, 'Ссылка входа устарела. Открой вход заново.'); await sendLoginMessage(chatId, 'Ссылка входа устарела.');
return; return;
} }
@@ -228,5 +240,5 @@ export async function handleTelegramBotWebhook(
}, },
}); });
await sendLoginMessage(chatId, 'Готово. Можно вернуться в MapFlow.', token); await sendLoginMessage(chatId, 'MapFlow', token, request.expiresAt);
} }