import type pg from "pg"; /** A row from the `users` column. */ export interface User { id: number; email: string; firstName: string; lastName: string; role: "gardener" | "owner"; } /** * Retrieves a user by their email and password. * * The password should be passed unaltered. All validation is done on the DB server. * * @returns The user, or `undefined` on authentication failure. */ export async function getUser( dbConn: pg.ClientBase, email: string, password: string, ): Promise { let result = await dbConn.query( "SELECT * FROM users WHERE email = $1 AND password_hash = crypt($2, password_hash);", [email, password], ); if (result.rowCount == 0) { return undefined; } return { id: result.rows[0].id, email: result.rows[0].email, firstName: result.rows[0].first_name, lastName: result.rows[0].last_name, role: result.rows[0].role, }; }