From 2d0634cdc3d00b3e55cf773ff03c7dc841112d36 Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Thu, 21 Dec 2023 21:22:47 +0000 Subject: 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 --- api/db/_setCurrentChallenge.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 api/db/_setCurrentChallenge.ts (limited to 'api/db/_setCurrentChallenge.ts') diff --git a/api/db/_setCurrentChallenge.ts b/api/db/_setCurrentChallenge.ts new file mode 100644 index 0000000..7d9450b --- /dev/null +++ b/api/db/_setCurrentChallenge.ts @@ -0,0 +1,35 @@ +import { sql } from '@vercel/postgres'; +import { CURRENT_CHALLENGES } from './_tables'; + +export default async function setCurrentChallenge(userId: string, challenge: string) { + try { + const insert = ` + INSERT INTO ${CURRENT_CHALLENGES.name} ( + ${CURRENT_CHALLENGES.fields.userId}, + ${CURRENT_CHALLENGES.fields.currentChallenge} + ) VALUES ( + ${userId}, + ${challenge} + ); + `); + + const update = ` + UPDATE TABLE ${CURRENT_CHALLENGES.name} + SET ${CURRENT_CHALLENGES.fields.currentChallenge} = ${challenge} + WHERE ${CURRENT_CHALLENGES.fields.userId} = '${userId}'; + `); + + await sql.query(` + BEGIN + IF EXISTS ( + SELECT ${CURRENT_CHALLENGES.fields.userId} FROM ${CURRENT_CHALLENGES.name} + WHERE ${CURRENT_CHALLENGES.fields.userId} = ${userId} + ) + ${update}; + ELSE + ${insert}; + ` + } catch (err) { + throw new Error(`Failed to set current challenge for user with ID ${userId} in database.`, err); + } +} \ No newline at end of file -- cgit v1.2.3