mirror of
https://github.com/whekin/household-bot.git
synced 2026-04-01 02:04:03 +00:00
feat(miniapp): add telegram-authenticated shell
This commit is contained in:
66
apps/miniapp/src/miniapp-api.ts
Normal file
66
apps/miniapp/src/miniapp-api.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { runtimeBotApiUrl } from './runtime-config'
|
||||
|
||||
export interface MiniAppSession {
|
||||
authorized: boolean
|
||||
member?: {
|
||||
displayName: string
|
||||
isAdmin: boolean
|
||||
}
|
||||
telegramUser?: {
|
||||
firstName: string | null
|
||||
username: string | null
|
||||
languageCode: string | null
|
||||
}
|
||||
reason?: string
|
||||
}
|
||||
|
||||
function apiBaseUrl(): string {
|
||||
const runtimeConfigured = runtimeBotApiUrl()
|
||||
if (runtimeConfigured) {
|
||||
return runtimeConfigured.replace(/\/$/, '')
|
||||
}
|
||||
|
||||
const configured = import.meta.env.VITE_BOT_API_URL?.trim()
|
||||
|
||||
if (configured) {
|
||||
return configured.replace(/\/$/, '')
|
||||
}
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
return 'http://localhost:3000'
|
||||
}
|
||||
|
||||
return window.location.origin
|
||||
}
|
||||
|
||||
export async function fetchMiniAppSession(initData: string): Promise<MiniAppSession> {
|
||||
const response = await fetch(`${apiBaseUrl()}/api/miniapp/session`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
initData
|
||||
})
|
||||
})
|
||||
|
||||
const payload = (await response.json()) as {
|
||||
ok: boolean
|
||||
authorized?: boolean
|
||||
member?: MiniAppSession['member']
|
||||
telegramUser?: MiniAppSession['telegramUser']
|
||||
reason?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(payload.error)
|
||||
}
|
||||
|
||||
return {
|
||||
authorized: payload.authorized === true,
|
||||
...(payload.member ? { member: payload.member } : {}),
|
||||
...(payload.telegramUser ? { telegramUser: payload.telegramUser } : {}),
|
||||
...(payload.reason ? { reason: payload.reason } : {})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user