diff --git a/database/users.ts b/database/users.ts new file mode 100644 index 0000000..b76e89e --- /dev/null +++ b/database/users.ts @@ -0,0 +1,52 @@ +import { cache } from 'react'; +import type { User } from '../migrations/00002-createTableUsers'; +import { sql } from './connect'; + +type UserWithPasswordHash = User & { + passwordHash: string; +}; + +export const getUserInsecure = cache(async (username: string) => { + const [user] = await sql` + SELECT + users.id, + users.username + FROM + users + WHERE + username = ${username.toLowerCase()} + `; + return user; +}); + +export const getUserWithPasswordHashInsecure = cache( + async (username: string) => { + const [user] = await sql` + SELECT + * + FROM + users + WHERE + username = ${username.toLowerCase()} + `; + return user; + }, +); + +export const createUserInsecure = cache( + async (username: string, passwordHash: string) => { + const [user] = await sql` + INSERT INTO + users (username, password_hash) + VALUES + ( + ${username.toLowerCase()}, + ${passwordHash} + ) + RETURNING + users.id, + users.username + `; + return user; + }, +);