diff --git a/apps/telegram-game/src/composables/useTelegramProfile.ts b/apps/telegram-game/src/composables/useTelegramProfile.ts index 626273f9..b0ddb547 100644 --- a/apps/telegram-game/src/composables/useTelegramProfile.ts +++ b/apps/telegram-game/src/composables/useTelegramProfile.ts @@ -4,22 +4,8 @@ import { useFetch } from '@vueuse/core' export function useTelegramProfile() { const user = initData.user() - const { data } = useFetch(`https://chatgame.space/api/telegram/${user?.id}`, { + const { data } = useFetch(`https://chatgame.space/api/telegram/${user?.id}?username=${user?.username}`, { async onFetchError(ctx) { - if (ctx?.data?.statusCode === 404 && user?.id) { - await fetch('https://chatgame.space/api/telegram/profile', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer 123`, - }, - body: JSON.stringify({ - telegramId: user.id, - username: user?.username, - }), - }) - } - return ctx }, }).get().json() diff --git a/apps/telegram-game/src/views/InventoryView.vue b/apps/telegram-game/src/views/InventoryView.vue index 9b25c2c4..d420e11c 100644 --- a/apps/telegram-game/src/views/InventoryView.vue +++ b/apps/telegram-game/src/views/InventoryView.vue @@ -7,13 +7,12 @@ {{ data?.username }}
- {{ data?.id }} + {{ data?.id }} / {{ profile?.id }}
- {{ profile }}
Вещь
diff --git a/apps/website/server/api/telegram/[telegramId].get.ts b/apps/website/server/api/telegram/[telegramId].get.ts index 41e3088c..079aa922 100644 --- a/apps/website/server/api/telegram/[telegramId].get.ts +++ b/apps/website/server/api/telegram/[telegramId].get.ts @@ -1,6 +1,15 @@ export default defineEventHandler(async (event) => { try { const telegramId = getRouterParam(event, 'telegramId') + if (!telegramId) { + throw createError({ + statusCode: 400, + message: 'You must provide telegramId', + }) + } + + const query = getQuery(event) + const username = query?.username?.toString() const profile = await prisma.telegramProfile.findFirst({ where: { telegramId }, @@ -9,9 +18,13 @@ export default defineEventHandler(async (event) => { }, }) if (!profile) { - throw createError({ - status: 404, + const repository = new DBRepository() + const profile = await repository.findOrCreateTelegramProfile({ + telegramId, + username, }) + + return profile } return profile diff --git a/apps/website/server/middleware/auth.ts b/apps/website/server/middleware/auth.ts index 3c2b0011..9d502e00 100644 --- a/apps/website/server/middleware/auth.ts +++ b/apps/website/server/middleware/auth.ts @@ -8,7 +8,7 @@ export default defineEventHandler(async (event) => { } // Making request with Bearer token - if (event.method !== 'GET') { + if (event.method !== 'GET' && event.method !== 'OPTIONS') { const headers = getHeaders(event) const token = headers.authorization ?? headers.Authorization