-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser.js
More file actions
51 lines (43 loc) · 1.24 KB
/
user.js
File metadata and controls
51 lines (43 loc) · 1.24 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
import pool from './db.js'
const bcrypt = await import('bcrypt');
class User {
static async register(username, email, password) {
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltRounds);
const query = {
text: `
INSERT INTO users (username, email, password)
VALUES ($1, $2, $3)
RETURNING *;
`,
values: [username, email, hashedPassword],
};
try {
const result = await pool.query(query);
return result.rows[0];
} catch (err) {
throw err;
}
}
static async login(username, password) {
const query = {
text: `
SELECT * FROM users
WHERE username = $1;
`,
values: [username],
};
try {
const result = await pool.query(query);
const user = result.rows[0];
if (user && await bcrypt.compare(password, user.password)) {
return user;
} else {
throw new Error('Invalid username or password');
}
} catch (err) {
throw err;
}
}
}
export default User;