Replace bot login polling with completion mutation
All checks were successful
Build and deploy Backend / build (push) Successful in 37s

This commit is contained in:
Ruslan Bakiev
2026-05-13 19:36:01 +07:00
parent 7973c44705
commit 7deedcb276
2 changed files with 14 additions and 12 deletions

View File

@@ -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({ const request = await prisma.telegramLoginRequest.findUnique({
where: { tokenHash: sha256Hex(token) }, where: { tokenHash: sha256Hex(token) },
include: { user: true }, include: { user: true },
}); });
if (!request) { if (!request) {
return { status: 'EXPIRED', sessionToken: null, user: null }; throw new Error('Telegram login token is invalid.');
} }
if (request.expiresAt <= new Date()) { if (request.expiresAt <= new Date()) {
@@ -178,11 +178,14 @@ export async function getTelegramBotLoginStatus(token: string) {
where: { id: request.id }, where: { id: request.id },
data: { status: 'EXPIRED' }, 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 { return {
status: request.status,
sessionToken: request.sessionToken, sessionToken: request.sessionToken,
user: request.user, user: request.user,
}; };

View File

@@ -6,8 +6,8 @@ import {
type TelegramLoginData, type TelegramLoginData,
} from '../auth/telegram.js'; } from '../auth/telegram.js';
import { import {
completeTelegramBotLogin,
createTelegramBotLogin, createTelegramBotLogin,
getTelegramBotLoginStatus,
} from '../auth/telegram-bot-login.js'; } from '../auth/telegram-bot-login.js';
import { import {
listNearbyPlaces, listNearbyPlaces,
@@ -106,16 +106,14 @@ export const schema = /* GraphQL */ `
expiresAt: String! expiresAt: String!
} }
type TelegramBotLoginStatus { type TelegramBotLoginSession {
status: String! sessionToken: String!
sessionToken: String user: User!
user: User
} }
type Query { type Query {
health: String! health: String!
me: User! me: User!
telegramBotLoginStatus(token: String!): TelegramBotLoginStatus!
places: [Place!]! places: [Place!]!
nearbyPlaces(input: NearbyPlacesInput!): [Place!]! nearbyPlaces(input: NearbyPlacesInput!): [Place!]!
voiceExperiences: [VoiceExperience!]! voiceExperiences: [VoiceExperience!]!
@@ -123,6 +121,7 @@ export const schema = /* GraphQL */ `
type Mutation { type Mutation {
startTelegramBotLogin: TelegramBotLoginPayload! startTelegramBotLogin: TelegramBotLoginPayload!
completeTelegramBotLogin(token: String!): TelegramBotLoginSession!
authenticateTelegram(input: AuthenticateTelegramInput!): AuthPayload! authenticateTelegram(input: AuthenticateTelegramInput!): AuthPayload!
authenticateTelegramLogin(input: AuthenticateTelegramLoginInput!): AuthPayload! authenticateTelegramLogin(input: AuthenticateTelegramLoginInput!): AuthPayload!
createVoiceExperience(input: CreateVoiceExperienceInput!): VoiceExperience! createVoiceExperience(input: CreateVoiceExperienceInput!): VoiceExperience!
@@ -137,8 +136,6 @@ export const resolvers = {
const graphqlContext = context as GraphqlContext; const graphqlContext = context as GraphqlContext;
return requireTelegramUser(graphqlContext); return requireTelegramUser(graphqlContext);
}, },
telegramBotLoginStatus: async (_: unknown, args: { token: string }) =>
getTelegramBotLoginStatus(args.token),
places: async (_: unknown, __: unknown, context: unknown) => { places: async (_: unknown, __: unknown, context: unknown) => {
const graphqlContext = context as GraphqlContext; const graphqlContext = context as GraphqlContext;
await requireTelegramUser(graphqlContext); await requireTelegramUser(graphqlContext);
@@ -161,6 +158,8 @@ export const resolvers = {
}, },
Mutation: { Mutation: {
startTelegramBotLogin: async () => createTelegramBotLogin(), startTelegramBotLogin: async () => createTelegramBotLogin(),
completeTelegramBotLogin: async (_: unknown, args: { token: string }) =>
completeTelegramBotLogin(args.token),
authenticateTelegram: async ( authenticateTelegram: async (
_: unknown, _: unknown,
args: { input: { initData: string } }, args: { input: { initData: string } },