Move secrets to config.json

This commit is contained in:
Aelita4 2024-06-01 22:07:11 +02:00
parent 52ec7c6f94
commit 7b0bbf1c0f
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
5 changed files with 14 additions and 12 deletions

1
.gitignore vendored
View File

@ -16,6 +16,7 @@ pnpm-debug.log*
# environment variables
.env
.env.production
config.json
# macOS-specific files
.DS_Store

View File

@ -1,12 +1,8 @@
import { MongoClient } from "mongodb";
import config from '../../../config.json'
if(!process.env.MONGODB_URI || !process.env.MONGODB_DB) {
throw new Error("Please define MONGODB_URI and MONGODB_DB environment variables inside .env");
}
const uri = process.env.MONGODB_URI;
const dbName = process.env.MONGODB_DB;
const uri = config.MONGODB_URI;
const dbName = config.MONGODB_DB;
const options = {};
const mongo = new MongoClient(uri, options);

View File

@ -3,6 +3,7 @@ import type { APIRoute } from "astro";
import type AccessToken from "../../../types/AccessToken";
import { createAccessToken } from "../../../lib/db/accessTokens";
import { getUserByNickOrEmail } from "../../../lib/db/users";
import config from '../../../../config.json';
import type { ObjectId } from "mongodb";
export const POST: APIRoute = async({ request }) => {
@ -33,7 +34,7 @@ export const POST: APIRoute = async({ request }) => {
}), { status: 401 }
)
if(token !== import.meta.env.MASTER_ACCESSTOKEN) return new Response(
if(token !== config.MASTER_ACCESSTOKEN) return new Response(
JSON.stringify({
code: 401,
message: "Unauthorized",

View File

@ -4,6 +4,8 @@ import NavBar from '../components/NavBar.astro';
import { getUserByNickOrEmail, updateLastLogin } from '../lib/db/users';
import config from '../../config.json';
import { verify } from 'argon2';
let error = "";
@ -26,7 +28,7 @@ if(Astro.request.method === "POST") {
const user = await getUserByNickOrEmail(username as string);
if(user !== null && await verify(user.password, password as string)) {
const sessionTime = import.meta.env.SESSION_TIME_MINUTES * 60;
const sessionTime = config.SESSION_TIME_MINUTES * 60;
const res = await fetch(`${Astro.url.origin}/api/auth/generateAccessToken`, {
method: 'POST',
@ -37,7 +39,7 @@ if(Astro.request.method === "POST") {
}),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + import.meta.env.MASTER_ACCESSTOKEN
'Authorization': 'Bearer ' + config.MASTER_ACCESSTOKEN
}
});

View File

@ -4,6 +4,8 @@ import NavBar from '../components/NavBar.astro';
import { createUser } from '../lib/db/users';
import config from '../../config.json';
let error = "";
if(Astro.request.method === "POST") {
@ -56,7 +58,7 @@ if(Astro.request.method === "POST") {
if(error === "") {
const user = await createUser(username, email, password);
const sessionTime = import.meta.env.SESSION_TIME_MINUTES * 60;
const sessionTime = config.SESSION_TIME_MINUTES * 60;
const res = await fetch(`${Astro.url.origin}/api/auth/generateAccessToken`, {
method: 'POST',
@ -67,7 +69,7 @@ if(Astro.request.method === "POST") {
}),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + import.meta.env.MASTER_ACCESSTOKEN
'Authorization': 'Bearer ' + config.MASTER_ACCESSTOKEN
}
});