refactor: cleaning up the centralisation of json reading

This commit is contained in:
Ravinou 2025-04-05 10:53:13 +02:00
commit ca8199ca33
No known key found for this signature in database
GPG key ID: EEEE670C40F6A4D7
11 changed files with 100 additions and 148 deletions

View file

@ -1,12 +1,9 @@
//Lib
import { promises as fs } from 'fs';
import path from 'path';
import { authOptions } from '../auth/[...nextauth]';
import { getServerSession } from 'next-auth/next';
import { NextApiRequest, NextApiResponse } from 'next';
import { BorgWarehouseUser } from '~/types/domain/config.types';
import { AppriseAlertResponse } from '~/types/api/notification.types';
import { getServerSession } from 'next-auth/next';
import { getUsersList } from '~/helpers/functions';
import { ErrorResponse } from '~/types/api/error.types';
import { AppriseAlertResponse } from '~/types/api/notification.types';
import { authOptions } from '../auth/[...nextauth]';
export default async function handler(
req: NextApiRequest,
@ -25,12 +22,7 @@ export default async function handler(
}
try {
//Read the users file
const jsonDirectory = path.join(process.cwd(), '/config');
const fileContent = await fs.readFile(jsonDirectory + '/users.json', 'utf8');
//Parse the usersList
const usersList: Array<BorgWarehouseUser> = JSON.parse(fileContent);
const usersList = await getUsersList();
//Verify that the user of the session exists
const user = usersList.find((u) => u.username === session.user?.name);

View file

@ -1,12 +1,9 @@
//Lib
import { promises as fs } from 'fs';
import path from 'path';
import { authOptions } from '../auth/[...nextauth]';
import { getServerSession } from 'next-auth/next';
import { NextApiRequest, NextApiResponse } from 'next';
import { AppriseModeDTO } from '~/types/api/notification.types';
import { getServerSession } from 'next-auth/next';
import { getUsersList } from '~/helpers/functions';
import { ErrorResponse } from '~/types/api/error.types';
import { BorgWarehouseUser } from '~/types/domain/config.types';
import { AppriseModeDTO } from '~/types/api/notification.types';
import { authOptions } from '../auth/[...nextauth]';
export default async function handler(
req: NextApiRequest,
@ -23,12 +20,7 @@ export default async function handler(
return;
}
try {
//Read the users file
const jsonDirectory = path.join(process.cwd(), '/config');
const fileContent = await fs.readFile(jsonDirectory + '/users.json', 'utf8');
//Parse the usersList
const usersList: Array<BorgWarehouseUser> = JSON.parse(fileContent);
const usersList = await getUsersList();
//Verify that the user of the session exists
const user = usersList.find((u) => u.username === session.user?.name);

View file

@ -1,12 +1,9 @@
//Lib
import { promises as fs } from 'fs';
import path from 'path';
import { authOptions } from '../auth/[...nextauth]';
import { getServerSession } from 'next-auth/next';
import { NextApiRequest, NextApiResponse } from 'next';
import { AppriseServicesDTO } from '~/types/api/notification.types';
import { getServerSession } from 'next-auth/next';
import { getUsersList } from '~/helpers/functions';
import { ErrorResponse } from '~/types/api/error.types';
import { BorgWarehouseUser } from '~/types/domain/config.types';
import { AppriseServicesDTO } from '~/types/api/notification.types';
import { authOptions } from '../auth/[...nextauth]';
export default async function handler(
req: NextApiRequest,
@ -23,12 +20,7 @@ export default async function handler(
return;
}
try {
//Read the users file
const jsonDirectory = path.join(process.cwd(), '/config');
const fileContent = await fs.readFile(jsonDirectory + '/users.json', 'utf8');
//Parse the usersList
const usersList: Array<BorgWarehouseUser> = JSON.parse(fileContent);
const usersList = await getUsersList();
//Verify that the user of the session exists
const user = usersList.find((u) => u.username === session.user?.name);

View file

@ -1,12 +1,10 @@
//Lib
import { promises as fs } from 'fs';
import path from 'path';
import { authOptions } from '../auth/[...nextauth]';
import { getServerSession } from 'next-auth/next';
import { NextApiRequest, NextApiResponse } from 'next';
import { EmailAlertDTO } from '~/types/api/notification.types';
import { getServerSession } from 'next-auth/next';
import { getUsersList } from '~/helpers/functions';
import { ErrorResponse } from '~/types/api/error.types';
import { BorgWarehouseUser } from '~/types/domain/config.types';
import { EmailAlertDTO } from '~/types/api/notification.types';
import { authOptions } from '../auth/[...nextauth]';
export default async function handler(
req: NextApiRequest,
@ -25,12 +23,7 @@ export default async function handler(
}
try {
//Read the users file
const jsonDirectory = path.join(process.cwd(), '/config');
const fileContent = await fs.readFile(jsonDirectory + '/users.json', 'utf8');
//Parse the usersList
const usersList: Array<BorgWarehouseUser> = JSON.parse(fileContent);
const usersList = await getUsersList();
//Verify that the user of the session exists
const user = usersList.find((u) => u.username === session.user?.name);

View file

@ -1,21 +1,13 @@
import { authOptions } from '../auth/[...nextauth]';
import { getServerSession } from 'next-auth/next';
import { NextApiRequest, NextApiResponse } from 'next';
import { promises as fs } from 'fs';
import path from 'path';
import { ErrorResponse, SuccessResponse } from '~/types/api/error.types';
import { BorgWarehouseUser } from '~/types/domain/config.types';
import { exec } from 'child_process';
import { NextApiRequest, NextApiResponse } from 'next';
import { getServerSession } from 'next-auth/next';
import { promisify } from 'util';
import { getUsersList } from '~/helpers/functions';
import { ErrorResponse, SuccessResponse } from '~/types/api/error.types';
import { authOptions } from '../auth/[...nextauth]';
const execAsync = promisify(exec);
const getUsersList = async (): Promise<BorgWarehouseUser[]> => {
const jsonDirectory = path.join(process.cwd(), '/config');
const fileContent = await fs.readFile(`${jsonDirectory}/users.json`, 'utf8');
return JSON.parse(fileContent);
};
const getAppriseServicesURLs = (services: string[]): string => services.join(' ');
const checkAppriseInstalled = async (): Promise<boolean> => {

View file

@ -1,10 +1,9 @@
//Lib
import fs from 'fs';
import NextAuth, { NextAuthOptions, RequestInternal, User } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import { verifyPassword } from '../../../helpers/functions/auth';
import fs from 'fs';
import path from 'path';
import { BorgWarehouseUser } from '~/types/domain/config.types';
import { getUsersList } from '~/helpers/functions';
import { verifyPassword } from '../../../helpers/functions/auth';
const logLogin = async (message: string, req: Partial<RequestInternal>, success = false) => {
const ipAddress = req.headers?.['x-forwarded-for'] || 'unknown';
@ -54,14 +53,7 @@ export const authOptions: NextAuthOptions = {
);
}
const usersData = await fs.promises.readFile(jsonDirectory + '/users.json', 'utf8');
const usersList = (() => {
try {
return JSON.parse(usersData) as BorgWarehouseUser[];
} catch (error) {
throw new Error('Failed to parse users.json. Please check its format.');
}
})();
const usersList = await getUsersList();
//Step 1 : does the user exist ?
const userIndex = usersList.map((user) => user.username).indexOf(username.toLowerCase());