Skip to content

Commit

Permalink
chore: create profile on get
Browse files Browse the repository at this point in the history
  • Loading branch information
hmbanan666 committed Dec 13, 2024
1 parent 562894a commit 9f351b5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
16 changes: 1 addition & 15 deletions apps/telegram-game/src/composables/useTelegramProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions apps/telegram-game/src/views/InventoryView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
{{ data?.username }}
</div>
<div class="tg-hint text-sm">
{{ data?.id }}
{{ data?.id }} / {{ profile?.id }}
</div>
</div>
</div>

<div class="grid grid-cols-4 gap-2">
{{ profile }}
<div v-for="item in items" :key="item.id" class="tg-section-bg aspect-square p-2 rounded-md">
<div>Вещь</div>
</div>
Expand Down
17 changes: 15 additions & 2 deletions apps/website/server/api/telegram/[telegramId].get.ts
Original file line number Diff line number Diff line change
@@ -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 },
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/website/server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9f351b5

Please sign in to comment.