diff --git a/.env.example b/.env.example index c597a30..3f7ebaa 100644 --- a/.env.example +++ b/.env.example @@ -1,28 +1,12 @@ # Please use .env.exmaple as an starting point. Rename it to .env and fill in the values, the application requrires it. # This is the default .env file. - -# S3 INFO -S3_ACCESS_KEY="" -S3_SECRET_KEY="" -S3_BUCKETNAME="" -S3_ENDPOINT="" - -# GITHUB OAUTH (NOT WORKING 4n) -NUXT_GITHUB_CLIENT_ID="" -NUXT_GITHUB_CLIENT_SECRET="" - # GLOBAL DATABASE POSTGRES_URL="" +# THE BELOW TWO IS NOW IN THE DB, PLEASE FOLOW THIS GUIDE: https://github.com/hpware/news-analyze?tab=readme-ov-file#notes # GROQ API KEY -GROQ_API_KEY="" +#GROQ_API_KEY="" # PASSWORD SALT -PASSWORD_HASH_SALT="" - -# CF TURNSTILE -NUXT_CF_TURNSTILE_SITE_KEY="" -NUXT_CF_TURNSTILE_SECRET_KEY="" - -NUXT_DEV_ENV=false +#PASSWORD_HASH_SALT="" diff --git a/README.md b/README.md index 000c27c..86c3cd6 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,20 @@ Beta (Beta Docker Image): https://newsbeta.20090526.xyz https://github.com/user-attachments/assets/29414c5d-3b2f-420d-93c0-95c14a15bbb7 +## Notes: +The enviroment vars are stored in the database, which is cursed, I know, but this is the only way to let the system access new envs sent by the user, so if you are trying to spin up a instence of this app you MUST put the postgres url in the .env & create a table using beekeeper studio (my choice for SQL editing, you can choose whatever you like), and after that you can create the entire database by using this api call, https://<>/api/create_database in your browser. +```sql + CREATE TABLE IF NOT EXISTS global_vars ( + NAME TEXT PRIMARY KEY NOT NULL, + VAR TEXT NOT NULL + ); + INSERT INTO global_vars(name, var) + VALUES ('groq_api_key', '<>'); + INSERT INTO global_vars(name, var) + VALUES ('password_hash_salt', '<>'); +``` +Replace `<>` with your actual api key, and also replace `<>` with a random salt you get by running this command on your Mac/Linux device (Windows idk) `openssl rand -base64 48`. + ## Issues: ### Onboarding: Onboarding is a must for most people that are using the app for the first time, but I want to do to via a non-video like system, however implementing the function in a already large repo is kinda hard. So later this week, I will just add a basic video onboarding system. @@ -59,13 +73,10 @@ This code is absolutly NOT designed to be spinned up at Vercel or Netlify, it ha ### The API returning outdated data from more than 5+ years: Here is the GitHub Issue: https://github.com/hpware/news-analyze/issues/2 -### Groq API not loading to .env for some reasons. -If the user did not load a GROQ api, the summerizing system will just fail outright. Fixing this rn. - ### When using the desktop in the dev env it pops up an error ![](/.github/README/error1.png) -For some reasons, Nuxt's dev env prev does not display this error, but with the newer ones, it started displaying this error, please run `./wipedev.sh` or `./wipedev.bat` and restart the dev server. (And this is only a temp fix, I have no idea how can I fix this, if you have a fix, please submit a PR thx.) +For some reasons, Nuxt's dev env prev does not display this error, but with the newer ones, it started displaying this error, please run `./wipedev.sh` or `./wipedev.bat` and restart the dev server. (And this is only a temp fix, I have no idea how can I fix this, if you have a fix, please submit a PR, thx.) ## Why? diff --git a/server/api/ai/summarize/[slug].ts b/server/api/ai/summarize/[slug].ts index 4bba3b2..d7fe8d5 100644 --- a/server/api/ai/summarize/[slug].ts +++ b/server/api/ai/summarize/[slug].ts @@ -1,6 +1,7 @@ import { Groq } from "groq-sdk"; import sql from "~/server/components/postgres"; import { checkIfUserHasCustomGroqKey } from "~/server/components/customgroqsystem"; +import getEnvFromDB from "~/server/components/getEnvFromDB"; export default defineEventHandler(async (event) => { const host = getRequestHost(event); @@ -16,8 +17,9 @@ export default defineEventHandler(async (event) => { apiKey: doesTheUserHasACustomGroqApiAndWhatIsIt.customApi, }); } else { + const groq_api_key = await getEnvFromDB("groq_api_key"); groqClient = new Groq({ - apiKey: process.env.NUXT_GROQ_API_KEY, + apiKey: groq_api_key, }); } const query = getQuery(event); diff --git a/server/api/create_database.ts b/server/api/create_database.ts index bf6fbfb..de5bbad 100644 --- a/server/api/create_database.ts +++ b/server/api/create_database.ts @@ -60,6 +60,12 @@ export default defineEventHandler(async (event) => { ) `; + const createGlobalVars = await sql` + CREATE TABLE IF NOT EXISTS global_vars ( + NAME TEXT PRIMARY KEY NOT NULL, + VAR TEXT NOT NULL + ) + `; return { createUsers: createUsers, usersList: usersList, @@ -67,5 +73,6 @@ export default defineEventHandler(async (event) => { createSources: createSources, createUserOtherData: createUserOtherData, createArticlesArchive: createArticlesArchive, + createGlobalVars: createGlobalVars, }; }); diff --git a/server/api/user/login.ts b/server/api/user/login.ts index 67dfd42..fbe3721 100644 --- a/server/api/user/login.ts +++ b/server/api/user/login.ts @@ -1,16 +1,11 @@ import sql from "~/server/components/postgres"; import { v4 as uuidv4 } from "uuid"; import argon2 from "argon2"; +import getEnvFromDB from "~/server/components/getEnvFromDB"; const defaultAvatarUrl = "https://s3.yhw.tw/news-analyze/avatar/default.png"; export default defineEventHandler(async (event) => { - const salt = process.env.PASSWORD_HASH_SALT; - if (!salt) { - return { - error: "SALT_NOT_FOUND", - }; - } const body = await readBody(event); const { username, password } = body; console.log(password); @@ -33,6 +28,7 @@ export default defineEventHandler(async (event) => { where username = ${username}`; console.log(fetchUserInfo[0]); if (fetchUserInfo.length === 0) { + const salt = await getEnvFromDB("password_hash_salt"); const hashedPassword = await argon2.hash(salt + password); const userUUID = uuidv4(); const createNewUser = await sql` diff --git a/server/components/getEnvFromDB.ts b/server/components/getEnvFromDB.ts new file mode 100644 index 0000000..0a8eae3 --- /dev/null +++ b/server/components/getEnvFromDB.ts @@ -0,0 +1,16 @@ +import sql from "~/server/components/postgres"; +interface variReturn { + name: string; + var: string; +} +export default async function (vari: string) { + const fetchVar = await sql` + SELECT * FROM global_vars + WHERE NAME = ${vari}`; + if (fetchVar.length === 0) { + throw new Error( + "Cannot find var in the database. Have you followed this? https://github.com/hpware/news-analyze?tab=readme-ov-file#notes", + ); + } + return fetchVar[0].var; +}