generated from azures04/Base-REST-API
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.
49 lines
1.4 KiB
JavaScript
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,
|
|
} |