From 8e3419cceb5f07fa70eeb7288f52cb30b1912f2c Mon Sep 17 00:00:00 2001 From: Victor Ejike Nwosu Date: Tue, 22 Oct 2024 16:21:16 +0200 Subject: [PATCH] Add users insecure database queries --- database/users.ts | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 database/users.ts 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; + }, +);