Files
Server/repositories/usersRepo.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

104 lines
3.0 KiB
JavaScript

const { pool } = require("../modules/database")
const { DefaultError } = require("../errors/errors")
async function findById(id) {
try {
const sql = "SELECT * FROM users WHERE id = ?"
const rows = await pool.query(sql, [id])
return rows[0] || null
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function findByIdentifier(identifier) {
try {
const sql = "SELECT * FROM users WHERE identifier = ?"
const rows = await pool.query(sql, [identifier])
return rows[0] || null
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function create(identifier, serverId, remoteId = null, displayName, avatarUrl) {
try {
const sql = "INSERT INTO users (identifier, serverId, remoteId, displayName, avatarUrl) VALUES (?, ?, ?, ?, ?) RETURNING *"
const rows = await pool.query(sql, [identifier, serverId, remoteId, displayName, avatarUrl])
return rows[0] || null
} catch (error) {
if (error.code && error.code == "ER_DUP_ENTRY") {
throw new DefaultError(401, "Identifier already taken.", "Identifier already assigned.")
}
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function remove(id) {
try {
const sql = "DELETE FROM users WHERE id = ?"
const rows = await pool.query(sql, [id])
return rows.affectedRows > 0
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function isLocal(id) {
const sql = "SELECT (remoteId IS NULL) AS isLocal FROM users WHERE id = ?"
const rows = await db.query(sql, [id])
if (rows.length === 0) {
throw new DefaultError(404, `User ${id} not found`)
}
return Boolean(rows[0].isLocal)
}
async function updateProfile(id, displayName, avatarUrl) {
try {
const fields = []
const params = { id }
if (displayName !== undefined) {
fields.push('`displayName` = :displayName')
params.displayName = displayName
}
if (avatarUrl !== undefined) {
fields.push('`avatarUrl` = :avatarUrl')
params.avatarUrl = avatarUrl
}
if (fields.length === 0) {
return findById(id)
}
const sql = `UPDATE users SET ${fields.join(', ')} WHERE id = :id`
await pool.query(sql, params)
return findById(id)
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function findByServerAndRemoteId(serverId, remoteId) {
try {
const sql = "SELECT * FROM users WHERE serverId = ? AND remoteId = ?"
const rows = await pool.query(sql, [serverId, remoteId])
return rows[0] || null
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
module.exports = {
create,
remove,
isLocal,
findById,
updateProfile,
findByIdentifier,
findByServerAndRemoteId
}