Update server/src/db.js

This commit is contained in:
2025-11-13 15:47:20 +00:00
parent 81c49145f0
commit 35a89bf5ad

View File

@@ -36,14 +36,11 @@ export async function init() {
/** /**
* Characters model * Characters model
* - username + passhash live here * - username + passhash live here
* - bcrypt provides salt + cost factor per hash * - bcrypt automatically salts and stores the salt + cost in the hash
*/ */
export const Characters = { export const Characters = {
// create a new character with username + password
async create(username, password) { async create(username, password) {
// bcrypt automatically generates a unique salt for each hash. const saltRounds = 12; // bump if you want more cost
// You can bump the rounds (12, 14, etc.) if you want more work factor.
const saltRounds = 12;
const passhash = await bcrypt.hash(password, saltRounds); const passhash = await bcrypt.hash(password, saltRounds);
const { rows } = await query( const { rows } = await query(
@@ -53,17 +50,16 @@ export const Characters = {
return rows[0]; return rows[0];
}, },
// verify username/password; return character row or null
async verify(username, password) { async verify(username, password) {
const { rows } = await query( const { rows } = await query(
'SELECT * FROM characters WHERE username=$1', 'SELECT * FROM characters WHERE username=$1',
[username] [username]
); );
const char = rows[0]; const ch = rows[0];
if (!char) return null; if (!ch) return null;
const ok = await bcrypt.compare(password, char.passhash); const ok = await bcrypt.compare(password, ch.passhash);
return ok ? char : null; return ok ? ch : null;
}, },
async getById(id) { async getById(id) {