Refactor user creation logic in auth.js

This commit is contained in:
CyferShepard
2024-03-14 22:43:21 +02:00
parent ec498d9b5d
commit ebc968d8ac
2 changed files with 7 additions and 6 deletions

View File

@@ -8,7 +8,7 @@ class Config {
const state = this.#getConfigState(config);
if (state < 1) {
return { error: "Config Details Not Found" };
return { state: 0, error: "Config Details Not Found" };
}
return {
@@ -39,7 +39,10 @@ class Config {
//state 2 = configured and user and api key set
if (Configured.length > 0 && Configured[0].APP_USER !== null) {
if (Configured[0].JF_API_KEY === null || (typeof Configured[0].JF_API_KEY === 'string' && Configured[0].JF_API_KEY.trim() === "")) {
if (
Configured[0].JF_API_KEY === null ||
(typeof Configured[0].JF_API_KEY === "string" && Configured[0].JF_API_KEY.trim() === "")
) {
//check if user is configured but API is not configured then return state 1
state = 1;
} else {

View File

@@ -57,13 +57,11 @@ router.post("/createuser", async (req, res) => {
const { username, password } = req.body;
const config = await new configClass().getConfig();
if (config.state < 2) {
if (config.state != null && config.state < 2) {
const user = { id: 1, username: username };
const hasConfig = await new configClass().getConfig();
let query = 'INSERT INTO app_config ("ID","APP_USER","APP_PASSWORD") VALUES (1,$1,$2)';
if (!hasConfig.error) {
if (config.state > 0) {
query = 'UPDATE app_config SET "APP_USER"=$1, "APP_PASSWORD"=$2';
}