import {z} from 'zod' export type AuthenticateUserPasswordlessDto = {token: string, recaptchaToken?: string | undefined, invitationToken?: string | undefined}; export type CreateApiResourcesAuthV1PasswordlessSmscodePostloginArgs = {body: AuthenticateUserPasswordlessDto}; export type CreateApiResourcesAuthV1PasswordlessSmscodePostloginOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userMFAWebAuthnDeviceResponse = z.object({id: z.string(), deviceType: z.enum(["Platform", "CrossPlatform"]), name: z.string()}); export const userMFAPhoneDeviceResponse = z.object({id: z.string(), phoneNumber: z.string()}); export const userMFAAuthenticatorAppResponse = z.object({id: z.string()}); export const userMFAEmailCodeResponse = z.object({email: z.string()}); export const userMFADevicesResponse = z.object({webauthn: z.array(userMFAWebAuthnDeviceResponse), phones: z.array(userMFAPhoneDeviceResponse), authenticators: z.array(userMFAAuthenticatorAppResponse), emails: z.array(userMFAEmailCodeResponse)}); export const authenticationResponseDto = z.object({tokenType: z.string().optional(), mfaRequired: z.boolean(), mfaToken: z.string().optional(), mfaEnrolled: z.boolean().optional(), mfaDevices: userMFADevicesResponse.optional(), mfaStrategies: z.object({}).optional(), qrCode: z.string().optional(), recoveryCode: z.string().optional(), accessToken: z.string(), refreshToken: z.string(), expiresIn: z.number(), expires: z.string(), userId: z.string().optional(), userEmail: z.string().optional(), emailVerified: z.boolean().optional(), isBreachedPassword: z.boolean().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * SMS code postlogin * * POST /resources/auth/v1/passwordless/smscode/postlogin */ export const createApiResourcesAuthV1PasswordlessSmscodePostlogin = async (args: CreateApiResourcesAuthV1PasswordlessSmscodePostloginArgs, options?: CreateApiResourcesAuthV1PasswordlessSmscodePostloginOptions) => { const {baseUrl = "http://localhost:8181", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/auth/v1/passwordless/smscode/postlogin${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 validateResponse(authenticationResponseDto, await res.json()) };