summaryrefslogtreecommitdiff
path: root/app/src/lib/server/users.ts
blob: 6998c6619dec60142ef6dc6393563199b16d9360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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(
	dbClient: pg.ClientBase,
	email: string,
	password: string,
): Promise<User | undefined> {
	let result = await dbClient.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,
	};
}