From e2fba3d5a103c46eef1dae935142508f4b9bbe58 Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Mon, 29 Jan 2024 11:55:49 +0000 Subject: Adds authentication endpoint functions to frontend lib/api --- src/lib/api.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/lib') diff --git a/src/lib/api.ts b/src/lib/api.ts index 0b52658..e8b429f 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,4 +1,5 @@ import type { + AuthenticationResponseJSON, PublicKeyCredentialCreationOptionsJSON, RegistrationResponseJSON, } from '@simplewebauthn/typescript-types'; @@ -90,3 +91,42 @@ type VerifyRegistrationResponseParams = { displayName: string; registrationResponse: RegistrationResponseJSON; }; + +export async function getAuthenticationOptions(params: GetAuthenticationOptionsParams) { + const queryString = new URLSearchParams(params).toString(); + return await fetch(`${backendUrl}/auth/authentication?${queryString}`, { + method: 'GET', + headers: { 'Content-Type': 'application/json' }, + }); +} + +type GetAuthenticationOptionsParams = { + userId: string; +}; + +export async function verifyAuthenticationResponse(params: VerifyAuthenticationResponseParams) { + return await fetch(`${backendUrl}/auth/authentication`, { + method: 'POST', + body: JSON.stringify(params), + headers: { 'Content-Type': 'application/json' }, + }); +} + +type VerifyAuthenticationResponseParams = { + userId: string; + authenticationResponse: AuthenticationResponseJSON; +}; + +async function typeResponse(response: Response): Promise> { + const typedResponse = { + ...response, + json: undefined, + body: (await response.json().catch(() => null)) as T, + }; + + delete typedResponse.json; + + return typedResponse; +} + +export type TypedResponse = Omit & { body: T }; -- cgit v1.2.3