summaryrefslogtreecommitdiff
path: root/app/src/lib/server/users.ts
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/lib/server/users.ts')
-rw-r--r--app/src/lib/server/users.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/app/src/lib/server/users.ts b/app/src/lib/server/users.ts
new file mode 100644
index 0000000..c233fda
--- /dev/null
+++ b/app/src/lib/server/users.ts
@@ -0,0 +1,38 @@
+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.PoolClient,
+ email: string,
+ password: string,
+): Promise<User | undefined> {
+ 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,
+ };
+}