fix(logging): updated entire server side to the new logging system
This commit is contained in:
@@ -4,7 +4,7 @@ import {users} from "../../../../database/schema/users.js";
|
||||
import {eq, sql} from "drizzle-orm";
|
||||
import {checkPassword} from "../utils/checkPassword.js";
|
||||
import {roleCheck} from "./userRoles/getUserAccess.js";
|
||||
import {log} from "../../logger/logger.js";
|
||||
import {createLog} from "../../logger/logger.js";
|
||||
|
||||
/**
|
||||
* Authenticate a user and return a JWT.
|
||||
@@ -40,6 +40,7 @@ export async function login(
|
||||
email: user[0].email,
|
||||
roles: roles || null,
|
||||
role: user[0].role || null, // this should be removed onces full migration to v2 is completed
|
||||
prod: btoa(`${username.toLowerCase()}:${password}`),
|
||||
};
|
||||
|
||||
// update the user last login
|
||||
@@ -49,10 +50,10 @@ export async function login(
|
||||
.set({lastLogin: sql`NOW()`})
|
||||
.where(eq(users.user_id, user[0].user_id))
|
||||
.returning({lastLogin: users.lastLogin});
|
||||
log.info(`Its been 5days since ${user[0].username} has logged in`);
|
||||
createLog("info", "lst", "auth", `Its been 5days since ${user[0].username} has logged in`);
|
||||
//]);
|
||||
} catch (error) {
|
||||
log.error(error, "There was an error updating the user last login");
|
||||
createLog("error", "lst", "auth", "There was an error updating the user last login");
|
||||
}
|
||||
|
||||
const token = sign({user: userData}, secret, {expiresIn: expiresIn * 60});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {eq, sql} from "drizzle-orm";
|
||||
import {db} from "../../../../../database/dbclient.js";
|
||||
import {users} from "../../../../../database/schema/users.js";
|
||||
import {log} from "../../../logger/logger.js";
|
||||
import {createLog} from "../../../logger/logger.js";
|
||||
import {createPassword} from "../../utils/createPassword.js";
|
||||
|
||||
const blacklistedTokens = new Set();
|
||||
@@ -17,10 +17,9 @@ function isTokenBlacklisted(token: string) {
|
||||
|
||||
export const updateProfile = async (user: any, data: any, token: string) => {
|
||||
if (isTokenBlacklisted(token)) {
|
||||
log.warn(`${user.username} is trying to use a black listed token`);
|
||||
createLog("warn", user.username, "auth", `${user.username} is trying to use a black listed token`);
|
||||
throw Error("This token was already used");
|
||||
}
|
||||
log.info(`${user.user_id}`);
|
||||
|
||||
//re salt and encrypt the password
|
||||
try {
|
||||
@@ -33,6 +32,11 @@ export const updateProfile = async (user: any, data: any, token: string) => {
|
||||
|
||||
blacklistToken(token);
|
||||
} catch (error) {
|
||||
log.error(error, "There was an error updating the users profile");
|
||||
createLog(
|
||||
"error",
|
||||
user.username,
|
||||
"auth",
|
||||
`Error: ${JSON.stringify(error)}, "There was an error updating the users profile"`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
||||
import {login} from "../controllers/login.js";
|
||||
import {log} from "../../logger/logger.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
@@ -78,7 +77,7 @@ app.openapi(route, async (c) => {
|
||||
|
||||
try {
|
||||
const {token, user} = await login(username.toLowerCase(), password);
|
||||
log.info({username: username}, "logged in");
|
||||
|
||||
// Set the JWT as an HTTP-only cookie
|
||||
//c.header("Set-Cookie", `auth_token=${token}; HttpOnly; Secure; Path=/; SameSite=None; Max-Age=3600`);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
||||
import {verify} from "hono/jwt";
|
||||
import {log} from "../../logger/logger.js";
|
||||
|
||||
import {authMiddleware} from "../middleware/authMiddleware.js";
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
@@ -89,7 +89,6 @@ session.openapi(
|
||||
|
||||
return c.json({data: {token: newToken, user: payload.user}}, 200);
|
||||
} catch (error) {
|
||||
log.error(error, "Failed session check, user must be logged out");
|
||||
return c.json({message: "Unauthorized"}, 401);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
|
||||
import {authMiddleware} from "../../middleware/authMiddleware.js";
|
||||
import {updateProfile} from "../../controllers/users/updateProfile.js";
|
||||
import {verify} from "hono/jwt";
|
||||
import {log} from "../../../logger/logger.js";
|
||||
import {createLog} from "../../../logger/logger.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
@@ -77,7 +77,7 @@ app.openapi(
|
||||
const payload = await verify(token, process.env.JWT_SECRET!);
|
||||
user = payload.user;
|
||||
} catch (error) {
|
||||
log.error(error, "Failed session check, user must be logged out");
|
||||
createLog("error", "lst", "auth", "Failed session check, user must be logged out");
|
||||
return c.json({message: "Unauthorized"}, 401);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import {db} from "../../../../database/dbclient.js";
|
||||
import {roles} from "../../../../database/schema/roles.js";
|
||||
import {log} from "../../logger/logger.js";
|
||||
import {createLog} from "../../logger/logger.js";
|
||||
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
|
||||
const newRoles = [
|
||||
{name: "viewer"},
|
||||
@@ -27,12 +27,17 @@ export const areRolesIn = async () => {
|
||||
.values(newRoles)
|
||||
.onConflictDoNothing() // this will only update the ones that are new :D
|
||||
.returning({name: roles.name});
|
||||
log.info(newRole, "Roles were just added due to missing them on server startup");
|
||||
createLog(
|
||||
"info",
|
||||
"lst",
|
||||
"auth",
|
||||
`${JSON.stringify(newRole)}, "Roles were just added due to missing them on server startup"`
|
||||
);
|
||||
} catch (error) {
|
||||
log.error(error, "There was an error adding new roles to the db");
|
||||
createLog("error", "lst", "auth", "There was an error adding new roles to the db");
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(error, "There was an error getting or adding new roles");
|
||||
createLog("error", "lst", "auth", "There was an error getting or adding new roles");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user