From 7deedcb276d6346fa12df8c312703d10010a1d11 Mon Sep 17 00:00:00 2001 From: Ruslan Bakiev <572431+veikab@users.noreply.github.com> Date: Wed, 13 May 2026 19:36:01 +0700 Subject: [PATCH] Replace bot login polling with completion mutation --- src/auth/telegram-bot-login.ts | 11 +++++++---- src/graphql/schema.ts | 15 +++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/auth/telegram-bot-login.ts b/src/auth/telegram-bot-login.ts index 00cb99d..66bf6f4 100644 --- a/src/auth/telegram-bot-login.ts +++ b/src/auth/telegram-bot-login.ts @@ -163,14 +163,14 @@ export async function createTelegramBotLogin() { }; } -export async function getTelegramBotLoginStatus(token: string) { +export async function completeTelegramBotLogin(token: string) { const request = await prisma.telegramLoginRequest.findUnique({ where: { tokenHash: sha256Hex(token) }, include: { user: true }, }); if (!request) { - return { status: 'EXPIRED', sessionToken: null, user: null }; + throw new Error('Telegram login token is invalid.'); } if (request.expiresAt <= new Date()) { @@ -178,11 +178,14 @@ export async function getTelegramBotLoginStatus(token: string) { where: { id: request.id }, data: { status: 'EXPIRED' }, }); - return { status: 'EXPIRED', sessionToken: null, user: null }; + throw new Error('Telegram login token is expired.'); + } + + if (request.status !== 'CONFIRMED' || !request.sessionToken || !request.user) { + throw new Error('Telegram login is not confirmed.'); } return { - status: request.status, sessionToken: request.sessionToken, user: request.user, }; diff --git a/src/graphql/schema.ts b/src/graphql/schema.ts index 80d2bc1..6800cb6 100644 --- a/src/graphql/schema.ts +++ b/src/graphql/schema.ts @@ -6,8 +6,8 @@ import { type TelegramLoginData, } from '../auth/telegram.js'; import { + completeTelegramBotLogin, createTelegramBotLogin, - getTelegramBotLoginStatus, } from '../auth/telegram-bot-login.js'; import { listNearbyPlaces, @@ -106,16 +106,14 @@ export const schema = /* GraphQL */ ` expiresAt: String! } - type TelegramBotLoginStatus { - status: String! - sessionToken: String - user: User + type TelegramBotLoginSession { + sessionToken: String! + user: User! } type Query { health: String! me: User! - telegramBotLoginStatus(token: String!): TelegramBotLoginStatus! places: [Place!]! nearbyPlaces(input: NearbyPlacesInput!): [Place!]! voiceExperiences: [VoiceExperience!]! @@ -123,6 +121,7 @@ export const schema = /* GraphQL */ ` type Mutation { startTelegramBotLogin: TelegramBotLoginPayload! + completeTelegramBotLogin(token: String!): TelegramBotLoginSession! authenticateTelegram(input: AuthenticateTelegramInput!): AuthPayload! authenticateTelegramLogin(input: AuthenticateTelegramLoginInput!): AuthPayload! createVoiceExperience(input: CreateVoiceExperienceInput!): VoiceExperience! @@ -137,8 +136,6 @@ export const resolvers = { const graphqlContext = context as GraphqlContext; return requireTelegramUser(graphqlContext); }, - telegramBotLoginStatus: async (_: unknown, args: { token: string }) => - getTelegramBotLoginStatus(args.token), places: async (_: unknown, __: unknown, context: unknown) => { const graphqlContext = context as GraphqlContext; await requireTelegramUser(graphqlContext); @@ -161,6 +158,8 @@ export const resolvers = { }, Mutation: { startTelegramBotLogin: async () => createTelegramBotLogin(), + completeTelegramBotLogin: async (_: unknown, args: { token: string }) => + completeTelegramBotLogin(args.token), authenticateTelegram: async ( _: unknown, args: { input: { initData: string } },