Fix registration

This commit is contained in:
Daniel Flanagan 2022-10-08 02:03:57 -05:00
parent 31310d61e1
commit 1099ea1785
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4

View file

@ -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<UserID | RegistrationError | null> = {
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 (