-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthService.js
More file actions
44 lines (37 loc) · 1.27 KB
/
authService.js
File metadata and controls
44 lines (37 loc) · 1.27 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
import db from './database.js';
import bcrypt from 'bcrypt';
import { CONFIG } from './config.js';
// authentication functions
export async function createUser(username, password) {
try {
// check if user already exists
const result = await db.query("SELECT * FROM users WHERE username = $1", [username]);
if (result.rows.length > 0) {
throw new Error('Username already registered.');
}
// hash password and create user
const hash = await bcrypt.hash(password, CONFIG.SALT_ROUNDS);
await db.query("INSERT INTO users (username, password, highscore) VALUES ($1, $2, 0)", [username, hash]);
return true;
} catch (error) {
console.error('Error creating user:', error);
throw error;
}
}
export async function findUserByUsername(username) {
try {
const result = await db.query("SELECT * FROM users WHERE username = $1", [username]);
return result.rows[0] || null;
} catch (error) {
console.error('Error finding user:', error);
return null;
}
}
export async function verifyPassword(password, hashedPassword) {
try {
return await bcrypt.compare(password, hashedPassword);
} catch (error) {
console.error('Error verifying password:', error);
return false;
}
}