diff options
| author | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-12-21 21:22:47 +0000 |
|---|---|---|
| committer | Joe Carstairs <jcarstairs@scottlogic.com> | 2024-01-29 10:46:46 +0000 |
| commit | 2d0634cdc3d00b3e55cf773ff03c7dc841112d36 (patch) | |
| tree | 424f0b9ce2907e3d58336a2b25233bf0f9bead9d /api/db/_getAuthenticator.ts | |
| parent | f44b2a82b74337c6424c576fdbf4c1376166e553 (diff) | |
33 / Users can sign up and log in with WebAuthn (#39)
* Installs @vercel/postgres
* Installs @simplewebauthn/server
* Installs @simplewebauthn/browser
* Git-ignores all files starting with .env
* Reorganises folders in API
* Defines User type
* Defines Subscription type
* Defines Authenticator type
* Sets up table definition file
* Can get user from database
* Can add user to database
* Can get user's current challenge
* Can set user's current challenge
* Can get user's authenticators from database
* Can get authenticator by ID from database
* Can add user authenticator to database
* Can update authenticator in database
* Defines Relying Party information
* Can generate registration options
* Can verify registration response
* Defines registration API endpoint
* Defines user API endpoint
* Reorganises API functions on frontend
* Can access authentication API functions on frontend
* Can generate authentication options
* Can verify authentication response
* Documents the registration flow
* Fix dev_csso
* Form styling
* WIP adds sign up page
Diffstat (limited to 'api/db/_getAuthenticator.ts')
| -rw-r--r-- | api/db/_getAuthenticator.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/api/db/_getAuthenticator.ts b/api/db/_getAuthenticator.ts new file mode 100644 index 0000000..55c4b2f --- /dev/null +++ b/api/db/_getAuthenticator.ts @@ -0,0 +1,29 @@ +import { sql } from '@vercel/postgres'; +import { AUTHENTICATORS } from './_tables'; +import Authenticator from '../types/Authenticator'; + +export default async function getAuthenticator< + T extends keyof Authenticator +>(id: string, fields: T[]): Promise<Pick<Authenticator, T>> { + try { + const result = await sql.query(` + SELECT ${fields.join(', ')} + FROM ${AUTHENTICATORS.name} + WHERE ${AUTHENTICATORS.fields.id} = '${id}'; + `); + + const row = result.rows.at(0); + if (!row) { + throw new Error(`No authenticator with ID ${id} found in database.`); + } + + const authenticator: Partial<Pick<Authenticator, T>> = {}; + for (const field in fields) { + // Assume type conversion is already done by node-postgres + authenticator[field] = row[AUTHENTICATORS.fields[field]]; + } + return authenticator as Pick<Authenticator, T>; + } catch (err) { + throw new Error(`Failed to get authenticator with ID ${id} from database.`, err); + } +}
\ No newline at end of file |
