From 1099ea1785e104585f5a44a0d50a1be58abc9253 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sat, 8 Oct 2022 02:03:57 -0500 Subject: [PATCH] Fix registration --- routes/register.tsx | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/routes/register.tsx b/routes/register.tsx index 1fe56d7..11068d0 100644 --- a/routes/register.tsx +++ b/routes/register.tsx @@ -1,6 +1,6 @@ import { Handlers, PageProps } from "$fresh/server.ts"; import { Page } from "@/components/Page.tsx"; -import { PostgresError, queryObject } from "@/db/mod.ts"; +import { createUser, PostgresError } from "@/db/mod.ts"; import { hash } from "https://deno.land/x/bcrypt@v0.4.1/mod.ts"; type UserID = string; @@ -21,31 +21,16 @@ export const handler: Handlers = { if (!password) { return await context.render({ message: "no password provided" }); } - const password_digest = await hash(password.toString()); + const passwordDigest = await hash(password.toString()); try { - const result = await queryObject<{ userId: string }>( - ` - with new_user as ( - insert into "user" (username, password_digest, status) - values ($username, $password_digest, 'unverified') - returning id as user_id - ), new_team as ( - insert into "team" (display_name) - values ($team_name) - returning id as team_id - ) - insert into "team_user" (user_id, team_id, status) - values ( - (select user_id from new_user), - (select team_id from new_team), - 'owner' - ) returning user_id - `, - { username, password_digest, team_name: `${username}'s First Team` }, - ); + const result = await createUser({ + username: username.toString(), + passwordDigest, + }); console.debug(result); if (!result) throw "insert failed"; - const { rows: [{ userId: id }] } = result; + const [user, _team] = result; + if (!user) throw "insert failed"; return await context.render(id); } catch (err) { if (