🏗️」 wip: refactor working

This commit is contained in:
2025-07-18 14:11:54 +02:00
parent 56f6164671
commit 1fb6d07d10
9 changed files with 397 additions and 133 deletions

View File

@ -1,55 +1,13 @@
import fastifyJWT from '@fastify/jwt';
import fastifyCookie from '@fastify/cookie';
import Database from 'better-sqlite3';
import bcrypt from 'bcrypt';
const RESERVED_USERNAMES = ['admin'];
var env = process.env.NODE_ENV || 'development';
import { register } from './register.js';
import { login } from './login.js';
import authDB from '../../utils/authDB.js'
const saltRounds = 10;
let database;
if (env === 'development') {
database = new Database(":memory:", { verbose: console.log });
} else {
var dbPath = process.env.DB_PATH || '/db/db.sqlite'
database = new Database(dbPath);
}
/**
* @description Can be used to prepare the database
*/
function prepareDB() {
database.exec(`
CREATE TABLE credentials (
username TEXT PRIMARY KEY,
passwordHash TEXT
) STRICT
`);
}
prepareDB()
const userCheck = database.prepare('SELECT EXISTS (SELECT 1 FROM credentials WHERE username = ?);');
const passwordQuery = database.prepare('SELECT passwordHash FROM credentials WHERE username = ?;');
const userAdd = database.prepare('INSERT INTO credentials (username, passwordHash) VALUES (?, ?)');
/**
* @description Can be used to check is a user exists in the database
* @param {string} name
*
* @returns {boolean}
*/
function checkUser(name) {
const result = userCheck.get(name);
const key = Object.keys(result)[0];
return result[key] === 1;
}
function isValidString(value) {
return typeof value === 'string' && value.trim() !== '';
}
authDB.prepareDB();
/**
* @param {import('fastify').FastifyInstance} fastify
@ -67,6 +25,19 @@ export default async function(fastify, options) {
});
fastify.register(fastifyCookie);
fastify.get('/me', async (request, reply) => {
try {
const token = request.cookies.token;
const decoded = await fastify.jwt.verify(token);
return { user: decoded.user };
} catch {
return reply.code(401).send({ error: 'Unauthorized' });
}
});
// GOOGLE sign in
fastify.post('/login', {
schema: {
body: {
@ -78,44 +49,7 @@ export default async function(fastify, options) {
}
}
}
}, async (request, reply) => {
try {
/** @type {{ user: string, password: string }} */
const { user, password } = request.body;
if (!checkUser(user) || user === 'admin') {
return reply.code(400).send({ error: "User does not exist" });
}
const query = passwordQuery.get(user);
const hash = query?.passwordHash;
if (!hash) {
return reply.code(500).send({ error: "No password was found" });
}
const compare = await bcrypt.compare(password, hash);
if (!compare) {
return reply.code(401).send({ error: "Incorrect password" });
}
const token = fastify.jwt.sign({ user });
return reply
.setCookie('token', token, {
httpOnly: true,
path: '/',
secure: env !== 'development',
sameSite: 'lax',
})
.code(200)
.send({ msg: "Login successful" });
} catch (err) {
fastify.log.error(err);
return reply.code(500).send({ error: "Internal server error" });
}
});
}, async (request, reply) => { return login(request, reply, fastify); });
fastify.post('/register', {
schema: {
@ -128,41 +62,5 @@ export default async function(fastify, options) {
}
}
}
}, async (request, reply) => {
try {
/** @type {{ user: string, password: string }} */
const { user, password } = request.body;
if (RESERVED_USERNAMES.includes(user)) {
return reply.code(400).send({ error: 'Reserved username' });
}
if (!isValidString(user) || !isValidString(password)) {
return reply.code(400).send({ error: 'Invalid username or password' });
} else if (checkUser(user) === true) {
return reply.code(400).send({ error: "User already exist" });
} else if (password.length <= 8) {
return reply.code(400).send({ error: "Password too short" });
} else if (password.length > 64) {
return reply.code(400).send({ error: "Password too long" });
}
const hash = await bcrypt.hash(password, saltRounds);
userAdd.run(user, hash);
return reply.code(200).send({ msg: 'Register successfuly' });
} catch (err) {
fastify.log.error(err);
return reply.code(500).send({ error: "Internal server error" });
}
});
fastify.get('/me', async (request, reply) => {
try {
const token = request.cookies.token;
const decoded = await fastify.jwt.verify(token);
return { user: decoded.user };
} catch {
return reply.code(401).send({ error: 'Unauthorized' });
}
});
}, async (request, reply) => { return register(request, reply, saltRounds, fastify); });
}