Add createUser helper, admin users API, and bootstrap endpoint for mmendoza
This commit is contained in:
@@ -14,6 +14,15 @@ export async function verifyPassword(password: string, hash: string) {
|
||||
return bcrypt.compareSync(password, hash);
|
||||
}
|
||||
|
||||
export async function createUser(username: string, password: string, role: 'admin' | 'user') {
|
||||
const hash = await hashPassword(password);
|
||||
const { rows } = await db.query(
|
||||
'INSERT INTO users (username, password_hash, role) VALUES ($1, $2, $3) ON CONFLICT (username) DO UPDATE SET password_hash = EXCLUDED.password_hash, role = EXCLUDED.role RETURNING id, username, role',
|
||||
[username, hash, role]
|
||||
);
|
||||
return rows[0];
|
||||
}
|
||||
|
||||
export async function authenticateUser(username: string, password: string) {
|
||||
const { rows } = await db.query('SELECT id, username, password_hash, role FROM users WHERE username = $1', [username]);
|
||||
if (rows.length === 0) return null;
|
||||
|
||||
Reference in New Issue
Block a user