import type { ClientParams, User } from './index' import { createFetcher } from '../lib/fetcher' export function createAuthClient({ siteId, baseUrl }: ClientParams) { const fetcher = createFetcher(siteId, `${baseUrl}/auth`) async function anonymous(user: string): Promise { return fetcher.get('/anonymous/login', { user, aud: siteId }) } async function email(email: string, username: string): Promise<(token: string) => Promise> { const EMAIL_SIGNIN_ENDPOINT = '/email/login' await fetcher.get(EMAIL_SIGNIN_ENDPOINT, { address: email, user: username }) return function tokenVerification(token: string): Promise { return fetcher.get(EMAIL_SIGNIN_ENDPOINT, { token }) } } async function telegram() { const TELEGRAM_SIGNIN_ENDPOINT = '/telegram/login' const { bot, token } = await fetcher.get<{ bot: string; token: string }>( TELEGRAM_SIGNIN_ENDPOINT ) return { bot, token, verify() { return fetcher.get(TELEGRAM_SIGNIN_ENDPOINT, { token }) }, } } async function logout(): Promise { return fetcher.get('/logout') } return { anonymous, email, telegram, logout, } }