export type TokenRequestModel = {email: string, password: string, remember: boolean}; export type CreateApiV1LoginTokenArgs = {body: TokenRequestModel}; export type CreateApiV1LoginTokenOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; /** * Returns a user token if the given email and password are correct. * * If the user has multi-factor authentication enabled, this endpoints sends the OTP (one-time password) and returns `{ mfaEnabled: true, mfaMethod: _User's_MFA_Method_ }`. * Another request to must be made to /v1/login/mfa/token with the correct OTP. * * POST /v1/login/token * * Also accepts auth: API Key */ export const createApiV1LoginToken = async (args: CreateApiV1LoginTokenArgs, options: CreateApiV1LoginTokenOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/login/token${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };