Move secrets to config.json
This commit is contained in:
parent
52ec7c6f94
commit
7b0bbf1c0f
|
@ -16,6 +16,7 @@ pnpm-debug.log*
|
||||||
# environment variables
|
# environment variables
|
||||||
.env
|
.env
|
||||||
.env.production
|
.env.production
|
||||||
|
config.json
|
||||||
|
|
||||||
# macOS-specific files
|
# macOS-specific files
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
import { MongoClient } from "mongodb";
|
import { MongoClient } from "mongodb";
|
||||||
|
import config from '../../../config.json'
|
||||||
|
|
||||||
|
const uri = config.MONGODB_URI;
|
||||||
if(!process.env.MONGODB_URI || !process.env.MONGODB_DB) {
|
const dbName = config.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 options = {};
|
const options = {};
|
||||||
|
|
||||||
const mongo = new MongoClient(uri, options);
|
const mongo = new MongoClient(uri, options);
|
||||||
|
|
|
@ -3,6 +3,7 @@ import type { APIRoute } from "astro";
|
||||||
import type AccessToken from "../../../types/AccessToken";
|
import type AccessToken from "../../../types/AccessToken";
|
||||||
import { createAccessToken } from "../../../lib/db/accessTokens";
|
import { createAccessToken } from "../../../lib/db/accessTokens";
|
||||||
import { getUserByNickOrEmail } from "../../../lib/db/users";
|
import { getUserByNickOrEmail } from "../../../lib/db/users";
|
||||||
|
import config from '../../../../config.json';
|
||||||
import type { ObjectId } from "mongodb";
|
import type { ObjectId } from "mongodb";
|
||||||
|
|
||||||
export const POST: APIRoute = async({ request }) => {
|
export const POST: APIRoute = async({ request }) => {
|
||||||
|
@ -33,7 +34,7 @@ export const POST: APIRoute = async({ request }) => {
|
||||||
}), { status: 401 }
|
}), { status: 401 }
|
||||||
)
|
)
|
||||||
|
|
||||||
if(token !== import.meta.env.MASTER_ACCESSTOKEN) return new Response(
|
if(token !== config.MASTER_ACCESSTOKEN) return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
code: 401,
|
code: 401,
|
||||||
message: "Unauthorized",
|
message: "Unauthorized",
|
||||||
|
|
|
@ -4,6 +4,8 @@ import NavBar from '../components/NavBar.astro';
|
||||||
|
|
||||||
import { getUserByNickOrEmail, updateLastLogin } from '../lib/db/users';
|
import { getUserByNickOrEmail, updateLastLogin } from '../lib/db/users';
|
||||||
|
|
||||||
|
import config from '../../config.json';
|
||||||
|
|
||||||
import { verify } from 'argon2';
|
import { verify } from 'argon2';
|
||||||
|
|
||||||
let error = "";
|
let error = "";
|
||||||
|
@ -26,7 +28,7 @@ if(Astro.request.method === "POST") {
|
||||||
const user = await getUserByNickOrEmail(username as string);
|
const user = await getUserByNickOrEmail(username as string);
|
||||||
|
|
||||||
if(user !== null && await verify(user.password, password 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`, {
|
const res = await fetch(`${Astro.url.origin}/api/auth/generateAccessToken`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -37,7 +39,7 @@ if(Astro.request.method === "POST") {
|
||||||
}),
|
}),
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': 'Bearer ' + import.meta.env.MASTER_ACCESSTOKEN
|
'Authorization': 'Bearer ' + config.MASTER_ACCESSTOKEN
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@ import NavBar from '../components/NavBar.astro';
|
||||||
|
|
||||||
import { createUser } from '../lib/db/users';
|
import { createUser } from '../lib/db/users';
|
||||||
|
|
||||||
|
import config from '../../config.json';
|
||||||
|
|
||||||
let error = "";
|
let error = "";
|
||||||
|
|
||||||
if(Astro.request.method === "POST") {
|
if(Astro.request.method === "POST") {
|
||||||
|
@ -56,7 +58,7 @@ if(Astro.request.method === "POST") {
|
||||||
if(error === "") {
|
if(error === "") {
|
||||||
const user = await createUser(username, email, password);
|
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`, {
|
const res = await fetch(`${Astro.url.origin}/api/auth/generateAccessToken`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -67,7 +69,7 @@ if(Astro.request.method === "POST") {
|
||||||
}),
|
}),
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': 'Bearer ' + import.meta.env.MASTER_ACCESSTOKEN
|
'Authorization': 'Bearer ' + config.MASTER_ACCESSTOKEN
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue