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({
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,
};

View File

@@ -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 } },