Skip to content

Commit

Permalink
fix(Middleware): more tries to fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andy1uu committed May 6, 2024
1 parent 9458a06 commit 0c726e4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MONGODB_URI="mongodb+srv://andyluu:RhTdUUNFlwrJdAGY@andyluu.uzkteb7.mongodb.net/?retryWrites=true&w=majority&appName=andyluu"
NODE_ENV=development
MONGODB_URI="mongodb+srv://andyluu:Aixw2YLVw7eddgAU@andyluu.uzkteb7.mongodb.net/?retryWrites=true&w=majority&appName=andyluu"
PORT=3000
DATABASE_NAME=andyluu
EDUCATION_DATABASE_NAME=education
API_URL=http://localhost:3000/api
API_KEY=MbQoaQQTAfThmN7c9e7nDYeQYY3pChMBiaybo3tq
REACT_APP_API_URL=http://localhost:3000/api
REACT_APP_API_KEY=MbQoaQQTAfThmN7c9e7nDYeQYY3pChMBiaybo3tq
3 changes: 2 additions & 1 deletion src/app/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import axios from "axios";

export default axios.create({
baseURL: process.env.API_URL,
baseURL: process.env.REACT_APP_API_URL,
headers: {
"Content-Type": "application/json",
"API_KEY": process.env.REACT_APP_API_KEY,
},
});
2 changes: 0 additions & 2 deletions src/app/api/education/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { limiter } from "../config/limiter";
import clientPromise from "../../../../lib/mongodb";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

export const GET = async (request: NextRequest, response: NextResponse) => {
try {

const client = await clientPromise;
const database = client.db(process.env.DATABASE_NAME);
const education = await database
Expand Down
1 change: 0 additions & 1 deletion src/app/education/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import EducationAPI from "./api";

async function getData() {
return await EducationAPI.getEducation().then((response) => {
console.log(response);
return response.data;
});
}
Expand Down
18 changes: 18 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

export const middleware = (request: NextRequest) => {
const apiKey = request.headers.get("API_KEY");

if (apiKey !== process.env.REACT_APP_API_KEY) {
return new NextResponse(null, {
status: 401,
statusText: "Invalid API Key",
headers: { "Content-Type": "application/json" },
});
}

return NextResponse.next();
};

export const config = { matcher: "/api/:path*" };

1 comment on commit 0c726e4

@vercel
Copy link

@vercel vercel bot commented on 0c726e4 May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.