import { dispatchMessage } from '../messageBus/messageBus.ts' import client from '../api/client.ts' import { ProblemDetails } from '../../types' import { SignupCode } from './signupCode.ts' export class AuthService { constructor() {} async login(username: string, password: string) { const res = await client.POST('/auth/login', { body: { username, password }, credentials: 'include', }) if (!res.data) { throw new Error('invalid credentials') } dispatchMessage('auth:logged-in', { ...res.data }) } async signup(username: string, password: string, signupCode: string) { const res = await client.POST('/auth/register', { body: { username, password, signupCode, email: null }, credentials: 'include', }) if (!res.data) { console.error(res.error) throw new Error((res.error as ProblemDetails)?.detail ?? 'invalid credentials') } dispatchMessage('auth:registered', { ...res.data }) } async logout() { await client.DELETE('/auth/session', { credentials: 'include' }) dispatchMessage('auth:logged-out', null) } async createSignupCode(code: string, email: string, name: string) { const res = await client.POST('/auth/signup-codes', { body: { code, email, name }, credentials: 'include', }) if (!res.data) { console.error(res.error) throw new Error('failed to create signup code') } } async listSignupCodes() { const res = await client.GET('/auth/signup-codes', { credentials: 'include', }) if (!res.data) { console.error(res.error) throw new Error('error') } return res.data.signupCodes.map(SignupCode.fromDto) } }