style(auth): format changes to the new config only

This commit is contained in:
2025-04-02 21:24:51 -05:00
parent 3355eb389c
commit b48dd8fa15
3 changed files with 90 additions and 64 deletions

View File

@@ -1,14 +1,22 @@
import {eq} from "drizzle-orm"; import { eq } from "drizzle-orm";
import {db} from "../../../../database/dbclient.js"; import { db } from "../../../../database/dbclient.js";
import {users} from "../../../../database/schema/users.js"; import { users } from "../../../../database/schema/users.js";
import {createPassword} from "../utils/createPassword.js"; import { createPassword } from "../utils/createPassword.js";
import {setSysAdmin} from "./userRoles/setSysAdmin.js"; import { setSysAdmin } from "./userRoles/setSysAdmin.js";
import { createLog } from "../../logger/logger.js";
export const registerUser = async (username: string, password: string, email: string) => { export const registerUser = async (
username: string,
password: string,
email: string
) => {
const usercount = await db.select().from(users); const usercount = await db.select().from(users);
// make sure the user dose not already exist in the system // make sure the user dose not already exist in the system
const userCheck = await db.select().from(users).where(eq(users.username, username)); const userCheck = await db
.select()
.from(users)
.where(eq(users.username, username));
if (userCheck.length === 1) { if (userCheck.length === 1) {
return { return {
@@ -25,19 +33,27 @@ export const registerUser = async (username: string, password: string, email: st
try { try {
const user = await db const user = await db
.insert(users) .insert(users)
.values({username, email, password}) .values({ username, email, password })
.returning({user: users.username, email: users.email}); .returning({ user: users.username, email: users.email });
if (usercount.length <= 1) { if (usercount.length <= 1) {
console.log(`${username} is the first user and will be set to system admin.`); createLog(
const updateUser = await db.select().from(users).where(eq(users.username, username)); "info",
"auth",
"auth",
`${username} is the first user and will be set to system admin.`
);
const updateUser = await db
.select()
.from(users)
.where(eq(users.username, username));
setSysAdmin(updateUser, "systemAdmin"); setSysAdmin(updateUser, "systemAdmin");
} }
return {sucess: true, message: "User Registered", user}; return { sucess: true, message: "User Registered", user };
} catch (error) { } catch (error) {
console.log(error); createLog("error", "auth", "auth", `${error}`);
return { return {
success: false, success: false,
message: `${username} already exists please login or reset password, if you feel this is an error please contact your admin.`, message: `${username} already exists please login or reset password, if you feel this is an error please contact your admin.`,

View File

@@ -9,63 +9,72 @@ import { responses } from "../../../../globalUtils/routeDefs/responses.js";
const app = new OpenAPIHono(); const app = new OpenAPIHono();
const responseSchema = z.object({ const responseSchema = z.object({
success: z.boolean().openapi({ example: true }), success: z.boolean().openapi({ example: true }),
message: z.string().optional().openapi({ example: "user access" }), message: z.string().optional().openapi({ example: "user access" }),
data: z.array(z.object({})).optional().openapi({ example: [] }), data: z.array(z.object({})).optional().openapi({ example: [] }),
}); });
const UserAccess = z.object({ const UserAccess = z.object({
username: z username: z
.string() .string()
.regex(/^[a-zA-Z0-9_]{3,30}$/) .regex(/^[a-zA-Z0-9_]{3,30}$/)
.openapi({ example: "smith034" }), .openapi({ example: "smith034" }),
module: z.string().openapi({ example: "production" }), module: z.string().openapi({ example: "production" }),
role: z.string().openapi({ example: "viewer" }), role: z.string().openapi({ example: "viewer" }),
override: z.string().optional().openapi({ example: "secretString" }), override: z.string().optional().openapi({ example: "secretString" }),
}); });
app.openapi( app.openapi(
createRoute({ createRoute({
tags: ["Auth:admin"], tags: ["Auth:admin"],
summary: "Sets Users access", summary: "Sets Users access",
method: "post", method: "post",
path: "/setuseraccess", path: "/setuseraccess",
middleware: [ middleware: [
authMiddleware, authMiddleware,
hasCorrectRole(["admin", "systemAdmin"], "admin"), hasCorrectRole(["admin", "systemAdmin"], "admin"),
], ],
description: "When logged in you will be able to grant new permissions", description: "When logged in you will be able to grant new permissions",
request: { request: {
body: { body: {
content: { content: {
"application/json": { schema: UserAccess }, "application/json": { schema: UserAccess },
},
},
}, },
}, responses: responses(),
}, }),
responses: responses(), async (c) => {
}), //apiHit(c, { endpoint: "api/auth/setUserRoles" });
async (c) => { const { username, module, role, override } = await c.req.json();
//apiHit(c, { endpoint: "api/auth/setUserRoles" }); try {
const { username, module, role, override } = await c.req.json(); const access = await setUserAccess(
try { username,
const access = await setUserAccess(username, module, role, override); module,
//return apiReturn(c, true, access?.message, access?.data, 200); role,
return c.json( override
{ success: access.success, message: access.message, data: access.data }, );
200 //return apiReturn(c, true, access?.message, access?.data, 200);
); return c.json(
} catch (error) { {
console.log(error); success: access.success,
//return apiReturn(c, false, "Error in setting the user access", error, 400); message: access.message,
return c.json( data: access.data,
{ },
success: false, 200
message: "Error in setting the user access", );
data: error, } catch (error) {
}, console.log(error);
400 //return apiReturn(c, false, "Error in setting the user access", error, 400);
); return c.json(
{
success: false,
message: "Error in setting the user access",
data: error,
},
400
);
}
} }
}
); );
export default app; export default app;

View File

@@ -9,6 +9,7 @@ import { createLog } from "../logger/logger.js";
import { note, notificationCreate } from "./utils/masterNotifications.js"; import { note, notificationCreate } from "./utils/masterNotifications.js";
import { startNotificationMonitor } from "./utils/processNotifications.js"; import { startNotificationMonitor } from "./utils/processNotifications.js";
import notifyStats from "./routes/getActiveNotifications.js"; import notifyStats from "./routes/getActiveNotifications.js";
const app = new OpenAPIHono(); const app = new OpenAPIHono();
const routes = [sendemail, notifyStats] as const; const routes = [sendemail, notifyStats] as const;