Files
Server/repositories/credentialsRepo.js
T
azures04 22542243f6 Add auth, JWT tokens, and bootstrap startup
Introduce bootstrap entrypoint and prestartup to ping DB and generate/manage RSA keypair. Add security module for key IO. Implement JWT-based access and refresh tokens (tokensService) with refresh_tokens table and repo. Add auth, provider and user services, and refresh-token management. Update repositories (users, credentials, providers, servers) with SQL fixes and new helper methods. Move DDL execution to bootstrap (remove from server). Update package.json main and add dependencies (bcryptjs, jsonwebtoken). Update .env.example and .gitignore accordingly.
2026-09-02 01:55:58 +02:00

49 lines
1.4 KiB
JavaScript

const { pool } = require("../modules/database")
const { DefaultError } = require("../errors/errors")
async function findByUserId(userId) {
try {
const sql = "SELECT * FROM credentials WHERE userId = ?"
const rows = await pool.query(sql, [userId])
return rows[0] || null
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function create(userId, passwordHash) {
try {
const sql = "INSERT INTO credentials (userId, passwordHash) VALUES (?, ?)"
const rows = await pool.query(sql, [userId, passwordHash])
return rows || null
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function updatePassword(userId, passwordHash) {
try {
const sql = "UPDATE credentials SET passwordHash = ? WHERE userId = ?"
const rows = await pool.query(sql, [passwordHash, userId])
return rows.affectedRows > 0
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function remove(id) {
try {
const sql = "DELETE FROM credentials WHERE userId = ?"
const rows = await pool.query(sql, [id])
return rows.affectedRows > 0
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
module.exports = {
create,
remove,
findByUserId,
updatePassword,
}