diff options
| author | Joe Carstairs <jcarstairs@scottlogic.com> | 2024-01-29 11:55:49 +0000 |
|---|---|---|
| committer | Joe Carstairs <jcarstairs@scottlogic.com> | 2024-01-29 11:55:49 +0000 |
| commit | e2fba3d5a103c46eef1dae935142508f4b9bbe58 (patch) | |
| tree | 99d80db9b21b8cdb4b36198d58fddca39fef4a6e /src/lib | |
| parent | 0ca256d5af00307b1c91a38b6aad2df2ed46fb24 (diff) | |
Adds authentication endpoint functions to frontend lib/api33-webauthn
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/api.ts | 40 |
1 files changed, 40 insertions, 0 deletions
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<T>(response: Response): Promise<TypedResponse<T>> { + const typedResponse = { + ...response, + json: undefined, + body: (await response.json().catch(() => null)) as T, + }; + + delete typedResponse.json; + + return typedResponse; +} + +export type TypedResponse<T> = Omit<Response, 'body' | 'json'> & { body: T }; |
