From d7d3c63d7f35fca0eb087796c27aafa81f7707c6 Mon Sep 17 00:00:00 2001 From: gibbyb Date: Sun, 6 Oct 2024 15:19:54 -0500 Subject: [PATCH] Clean up apis --- .env.example | 0 .eslintrc.cjs | 0 .gitignore | 0 drizzle.config.ts | 0 next.config.js | 0 package.json | 0 pnpm-lock.yaml | 0 postcss.config.cjs | 0 prettier.config.js | 0 public/favicon.png | Bin .../createOrUpdateCountdown/route.ts | 15 ++++++---- .../getCountdownByRelationship/route.ts | 24 +++++++-------- src/app/api/messages/fetchMessages/route.ts | 28 ++++++++---------- src/app/api/messages/sendMessage/route.ts | 15 ++++------ .../api/relationships/createRequest/route.ts | 19 +++++------- .../relationships/deleteRelationship/route.ts | 24 +++++---------- .../api/relationships/updateRequest/route.ts | 19 +++++------- src/app/api/updatePushToken/route.ts | 16 +++++----- src/app/api/users/changePassword/route.ts | 16 +++++----- src/app/api/users/createUser/route.ts | 20 ++++--------- src/app/api/users/getUserByID/route.ts | 23 +++++++------- src/app/api/users/getUserByUsername/route.ts | 18 +++++------ src/app/api/users/login/route.ts | 18 +++++------ src/app/api/users/logout/route.ts | 2 +- src/app/api/users/refreshToken/route.ts | 2 +- src/app/api/users/updatePFP/route.ts | 19 +++++------- src/app/api/users/updatePushToken/route.ts | 20 +++++++------ src/app/layout.tsx | 0 src/app/page.tsx | 0 src/env.js | 0 src/middleware.ts | 19 ++++++++---- src/server/db/index.ts | 0 src/server/db/schema.ts | 0 src/server/functions.ts | 0 src/styles/globals.css | 0 tailwind.config.ts | 0 tsconfig.json | 0 37 files changed, 142 insertions(+), 175 deletions(-) mode change 100755 => 100644 .env.example mode change 100755 => 100644 .eslintrc.cjs mode change 100755 => 100644 .gitignore mode change 100755 => 100644 drizzle.config.ts mode change 100755 => 100644 next.config.js mode change 100755 => 100644 package.json mode change 100755 => 100644 pnpm-lock.yaml mode change 100755 => 100644 postcss.config.cjs mode change 100755 => 100644 prettier.config.js mode change 100755 => 100644 public/favicon.png mode change 100755 => 100644 src/app/layout.tsx mode change 100755 => 100644 src/app/page.tsx mode change 100755 => 100644 src/env.js mode change 100755 => 100644 src/server/db/index.ts mode change 100755 => 100644 src/server/db/schema.ts mode change 100755 => 100644 src/server/functions.ts mode change 100755 => 100644 src/styles/globals.css mode change 100755 => 100644 tailwind.config.ts mode change 100755 => 100644 tsconfig.json diff --git a/.env.example b/.env.example old mode 100755 new mode 100644 diff --git a/.eslintrc.cjs b/.eslintrc.cjs old mode 100755 new mode 100644 diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/drizzle.config.ts b/drizzle.config.ts old mode 100755 new mode 100644 diff --git a/next.config.js b/next.config.js old mode 100755 new mode 100644 diff --git a/package.json b/package.json old mode 100755 new mode 100644 diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml old mode 100755 new mode 100644 diff --git a/postcss.config.cjs b/postcss.config.cjs old mode 100755 new mode 100644 diff --git a/prettier.config.js b/prettier.config.js old mode 100755 new mode 100644 diff --git a/public/favicon.png b/public/favicon.png old mode 100755 new mode 100644 diff --git a/src/app/api/countdown/createOrUpdateCountdown/route.ts b/src/app/api/countdown/createOrUpdateCountdown/route.ts index 6faf5a9..fff07eb 100644 --- a/src/app/api/countdown/createOrUpdateCountdown/route.ts +++ b/src/app/api/countdown/createOrUpdateCountdown/route.ts @@ -4,15 +4,18 @@ import type { NextRequest } from "next/server"; import { createOrUpdateCountdown } from "~/server/functions"; import { middleware } from "~/middleware"; +type CreateOrUpdateCountdownRequest = { + relationshipId: string; + title: string; + date: string; +} + export const POST = async (request: NextRequest) => { - const middlewareResponse = middleware(request); + const middlewareResponse = await middleware(request); if (middlewareResponse) return middlewareResponse; try { - const { relationshipId, title, date } = await request.json() as { - relationshipId: string; - title: string; - date: string; - }; + const { relationshipId, title, date } = + await request.json()as CreateOrUpdateCountdownRequest; await createOrUpdateCountdown(Number.parseInt(relationshipId), title, new Date(date)); return NextResponse.json({ message: "Countdown created or updated successfully" }); } catch (error) { diff --git a/src/app/api/countdown/getCountdownByRelationship/route.ts b/src/app/api/countdown/getCountdownByRelationship/route.ts index 18aa55d..78dc60a 100644 --- a/src/app/api/countdown/getCountdownByRelationship/route.ts +++ b/src/app/api/countdown/getCountdownByRelationship/route.ts @@ -1,21 +1,19 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { getCountdownByRelationship } from "~/server/functions"; +import { middleware } from "~/middleware"; -export const GET = async (request: Request) => { +export const GET = async (request: NextRequest) => { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const url = new URL(request.url); - const apiKey = url.searchParams.get("apiKey"); - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } else { - const relationshipId = url.searchParams.get("relationshipId"); - if (!relationshipId) - return NextResponse.json({ message: "Invalid relationshipId" }, { status: 400 }); - const countdown = await getCountdownByRelationship(parseInt(relationshipId)); - return NextResponse.json(countdown); - } + const { searchParams } = new URL(request.url); + const relationshipId: string | null = searchParams.get("relationshipId"); + if (!relationshipId || isNaN(parseInt(relationshipId))) + return NextResponse.json({ message: "Invalid relationshipId" }, { status: 400 }); + const countdown = await getCountdownByRelationship(parseInt(relationshipId)); + return NextResponse.json(countdown); } catch (error) { console.error(error); return NextResponse.json({ message: "Error" }, { status: 500 }); diff --git a/src/app/api/messages/fetchMessages/route.ts b/src/app/api/messages/fetchMessages/route.ts index 6518aaf..3c0433f 100644 --- a/src/app/api/messages/fetchMessages/route.ts +++ b/src/app/api/messages/fetchMessages/route.ts @@ -1,24 +1,20 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { fetchMessages } from "~/server/functions"; +import { middleware } from "~/middleware"; -export const GET = async (request: Request) => { +export const GET = async (request: NextRequest) => { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const url = new URL(request.url); - const apiKey = url.searchParams.get("apiKey"); - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } else { - const userId = url.searchParams.get("userId"); - if (!userId) - return NextResponse.json({ message: "Invalid userId" }, { status: 400 }); - const partnerId = url.searchParams.get("partnerId"); - if (!partnerId) - return NextResponse.json({ message: "Invalid partnerId" }, { status: 400 }); - const messages = await fetchMessages(parseInt(userId), parseInt(partnerId)); - return NextResponse.json(messages); - } + const { searchParams } = new URL(request.url); + const userId: string | null = searchParams.get("userId"); + const partnerId: string | null = searchParams.get("partnerId"); + if (!userId || !partnerId || isNaN(parseInt(userId)) || isNaN(parseInt(partnerId))) + return NextResponse.json({ message: "Invalid userId or partnerId" }, { status: 400 }); + const messages = await fetchMessages(parseInt(userId), parseInt(partnerId)); + return NextResponse.json(messages); } catch (error) { console.error(error); return NextResponse.json({ message: "Error" }, { status: 500 }); diff --git a/src/app/api/messages/sendMessage/route.ts b/src/app/api/messages/sendMessage/route.ts index 7335304..009d3c1 100644 --- a/src/app/api/messages/sendMessage/route.ts +++ b/src/app/api/messages/sendMessage/route.ts @@ -2,6 +2,7 @@ import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; import { sendMessage } from "~/server/functions"; +import { middleware } from "~/middleware"; type sendMessageRequest = { senderId: number; @@ -12,24 +13,18 @@ type sendMessageRequest = { }; export const POST = async (request: NextRequest) => { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const url = new URL(request.url); - const apiKey = url.searchParams.get("apiKey"); - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } - const body = await request.json() as sendMessageRequest; const { senderId, receiverId, content, mediaUrl = "", mediaType = "text", - } = body; - if (!senderId || !receiverId || !content) { + } = await request.json() as sendMessageRequest; + if (!senderId || !receiverId || !content || isNaN(senderId) || isNaN(receiverId)) return NextResponse.json({ message: "Missing required fields" }, { status: 400 }); - } const message = await sendMessage(senderId, receiverId, content, mediaUrl, mediaType); return NextResponse.json(message, { status: 201 }); } catch (error) { diff --git a/src/app/api/relationships/createRequest/route.ts b/src/app/api/relationships/createRequest/route.ts index 51f1385..a530bab 100644 --- a/src/app/api/relationships/createRequest/route.ts +++ b/src/app/api/relationships/createRequest/route.ts @@ -1,26 +1,23 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { createRelationshipRequest } from "~/server/functions"; +import { middleware } from "~/middleware"; type CreateRequestRequest = { - apiKey: string; userId: number; targetUserId: number; } -export async function POST(request: Request) { +export async function POST(request: NextRequest) { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const { apiKey, userId, targetUserId } = await request.json() as CreateRequestRequest; - console.log("Received request:", { apiKey, userId, targetUserId }); + const { userId, targetUserId } = await request.json() as CreateRequestRequest; + console.log("Received request:", { userId, targetUserId }); - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } - - if (!userId || !targetUserId) { + if (!userId || !targetUserId || isNaN(userId) || isNaN(targetUserId)) return NextResponse.json({ message: "Missing required fields" }, { status: 400 }); - } console.log("Creating relationship request for user:", userId); const result = await createRelationshipRequest(userId, targetUserId); diff --git a/src/app/api/relationships/deleteRelationship/route.ts b/src/app/api/relationships/deleteRelationship/route.ts index a833b79..eadaabe 100644 --- a/src/app/api/relationships/deleteRelationship/route.ts +++ b/src/app/api/relationships/deleteRelationship/route.ts @@ -1,28 +1,20 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { deleteRelationship } from "~/server/functions"; +import { middleware } from "~/middleware"; -interface DeleteRelationshipRequest { - apiKey: string; - relationshipId: number; -} - -export async function POST(request: Request) { +export async function POST(request: NextRequest) { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const { apiKey, relationshipId } = await request.json() as DeleteRelationshipRequest; - console.log("Received request:", { apiKey, relationshipId }); + const { relationshipId } = await request.json() as { relationshipId: string }; - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } - - if (!relationshipId) { + if (!relationshipId || isNaN(parseInt(relationshipId))) return NextResponse.json({ message: "Missing required fields" }, { status: 400 }); - } console.log("Deleting relationship:", relationshipId); - const result = await deleteRelationship(relationshipId); + const result = await deleteRelationship(parseInt(relationshipId)); if (result.success) { console.log("Relationship deleted successfully"); diff --git a/src/app/api/relationships/updateRequest/route.ts b/src/app/api/relationships/updateRequest/route.ts index c7770ae..e255393 100644 --- a/src/app/api/relationships/updateRequest/route.ts +++ b/src/app/api/relationships/updateRequest/route.ts @@ -1,26 +1,23 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { updateRelationshipRequest } from "~/server/functions"; +import { middleware } from "~/middleware"; interface UpdateRequestRequest { - apiKey: string; relationshipId: number; status: 'accepted' | 'rejected'; } -export async function POST(request: Request) { +export async function POST(request: NextRequest) { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const { apiKey, relationshipId, status } = await request.json() as UpdateRequestRequest; - console.log("Received request:", { apiKey, relationshipId, status }); + const { relationshipId, status } = await request.json() as UpdateRequestRequest; + console.log("Received request:", { relationshipId, status }); - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } - - if (!relationshipId || !status) { + if (!relationshipId || !status || isNaN(relationshipId)) return NextResponse.json({ message: "Missing required fields" }, { status: 400 }); - } console.log("Updating relationship request:", relationshipId); const result = await updateRelationshipRequest(relationshipId, status); diff --git a/src/app/api/updatePushToken/route.ts b/src/app/api/updatePushToken/route.ts index 76f171f..6dd80e8 100644 --- a/src/app/api/updatePushToken/route.ts +++ b/src/app/api/updatePushToken/route.ts @@ -1,21 +1,19 @@ import { NextResponse } from 'next/server'; +import type { NextRequest } from 'next/server'; import { updateUserPushToken } from '~/server/functions'; +import { middleware } from "~/middleware"; type Data = { - apiKey: string; userId: string; pushToken: string; }; -export const POST = async (request: Request) => { +export const POST = async (request: NextRequest) => { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const { apiKey, userId, pushToken } = await request.json() as Data; - console.log('Received request:', { apiKey, userId, pushToken }); - - if (apiKey !== process.env.API_KEY) { - console.log('Invalid API Key'); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } + const { userId, pushToken } = await request.json() as Data; + console.log('Received request:', { userId, pushToken }); console.log('Updating push token for user:', userId); await updateUserPushToken(parseInt(userId), pushToken); diff --git a/src/app/api/users/changePassword/route.ts b/src/app/api/users/changePassword/route.ts index 84d3a23..a0c8b8d 100644 --- a/src/app/api/users/changePassword/route.ts +++ b/src/app/api/users/changePassword/route.ts @@ -1,23 +1,21 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { changePassword } from "~/server/functions"; +import { middleware } from "~/middleware"; type Data = { - apiKey: string; userId: number; oldPassword: string; newPassword: string; }; -export const POST = async (request: Request) => { +export const POST = async (request: NextRequest) => { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const { apiKey, userId, oldPassword, newPassword } = await request.json() as Data; - console.log("Received request:", { apiKey, userId, oldPassword, newPassword }); - - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } + const { userId, oldPassword, newPassword } = await request.json() as Data; + console.log("Received request:", { userId, oldPassword, newPassword }); console.log("Changing password for user:", userId); await changePassword(userId, oldPassword, newPassword); diff --git a/src/app/api/users/createUser/route.ts b/src/app/api/users/createUser/route.ts index b55095c..52a9184 100644 --- a/src/app/api/users/createUser/route.ts +++ b/src/app/api/users/createUser/route.ts @@ -1,6 +1,8 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { createUser } from "~/server/functions"; +import { middleware } from "~/middleware"; type CreateUserRequest = { username: string; @@ -11,20 +13,10 @@ type CreateUserRequest = { pushToken?: string; }; -export async function POST(request: Request) { +export async function POST(request: NextRequest) { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const url = new URL(request.url); - const apiKey = url.searchParams.get("apiKey"); - - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } - - // Parse the request body instead of using URL parameters for POST requests - const body = await request.json() as CreateUserRequest; - - // Destructure the body with default values for optional fields const { username, email, @@ -32,7 +24,7 @@ export async function POST(request: Request) { name, pfpURL = "", pushToken = "" - } = body; + } = await request.json() as CreateUserRequest; // Validate required fields if (!username || !email || !passwordHash || !name) { diff --git a/src/app/api/users/getUserByID/route.ts b/src/app/api/users/getUserByID/route.ts index 990595e..8a12004 100644 --- a/src/app/api/users/getUserByID/route.ts +++ b/src/app/api/users/getUserByID/route.ts @@ -1,22 +1,19 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { getUserById } from "~/server/functions"; +import { middleware } from "~/middleware"; -export const GET = async (request: Request) => { +export const GET = async (request: NextRequest) => { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { const url = new URL(request.url); - const apiKey = url.searchParams.get("apiKey"); - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } else { - const userId = url.searchParams.get("userId"); - if (!userId) { - return NextResponse.json({ message: "Invalid userId" }, { status: 400 }); - } - const user = await getUserById(parseInt(userId)); - return NextResponse.json(user); - } + const userId = url.searchParams.get("userId"); + if (!userId || isNaN(parseInt(userId))) + return NextResponse.json({ message: "Invalid userId" }, { status: 400 }); + const user = await getUserById(parseInt(userId)); + return NextResponse.json(user); } catch (error) { console.error(error); return NextResponse.json({ message: "Error" }, { status: 500 }); diff --git a/src/app/api/users/getUserByUsername/route.ts b/src/app/api/users/getUserByUsername/route.ts index 0be6895..285e01e 100644 --- a/src/app/api/users/getUserByUsername/route.ts +++ b/src/app/api/users/getUserByUsername/route.ts @@ -1,19 +1,17 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { getUserByUsername } from "~/server/functions"; +import { middleware } from "~/middleware"; -export const GET = async (request: Request) => { +export const GET = async (request: NextRequest) => { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { const url = new URL(request.url); - const apiKey = url.searchParams.get("apiKey"); - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } else { - const username = url.searchParams.get("username") ?? "2"; - const user = await getUserByUsername(username); - return NextResponse.json(user); - } + const username = url.searchParams.get("username") ?? "2"; + const user = await getUserByUsername(username); + return NextResponse.json(user); } catch (error) { console.error(error); return NextResponse.json({ message: "Error" }, { status: 500 }); diff --git a/src/app/api/users/login/route.ts b/src/app/api/users/login/route.ts index 1e2c2b0..3516472 100644 --- a/src/app/api/users/login/route.ts +++ b/src/app/api/users/login/route.ts @@ -1,22 +1,20 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { login } from "~/server/functions"; +import { middleware } from "~/middleware"; -interface LoginRequest { - apiKey: string; +type LoginRequest = { username: string; password: string; } -export async function POST(request: Request) { +export async function POST(request: NextRequest) { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const { apiKey, username, password } = await request.json() as LoginRequest; - console.log("Received request:", { apiKey, username, password }); - - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } + const { username, password } = await request.json() as LoginRequest; + console.log("Received request:", { username, password }); if (!username || !password) { return NextResponse.json({ message: "Missing required fields" }, { status: 400 }); diff --git a/src/app/api/users/logout/route.ts b/src/app/api/users/logout/route.ts index efcea40..d5c5972 100644 --- a/src/app/api/users/logout/route.ts +++ b/src/app/api/users/logout/route.ts @@ -6,7 +6,7 @@ import { middleware } from "~/middleware"; import jwt from "jsonwebtoken"; export const POST = async (request: NextRequest) => { - const middlewareResponse = middleware(request); + const middlewareResponse = await middleware(request); if (middlewareResponse) return middlewareResponse; try { diff --git a/src/app/api/users/refreshToken/route.ts b/src/app/api/users/refreshToken/route.ts index 6a01297..f0f9e83 100644 --- a/src/app/api/users/refreshToken/route.ts +++ b/src/app/api/users/refreshToken/route.ts @@ -5,7 +5,7 @@ import { refreshToken } from "~/server/functions"; import { middleware } from "~/middleware"; export async function POST(request: NextRequest) { - const middlewareResponse = middleware(request); + const middlewareResponse = await middleware(request); if (middlewareResponse) return middlewareResponse; try { const { refreshToken: token } = await request.json() as { refreshToken: string }; diff --git a/src/app/api/users/updatePFP/route.ts b/src/app/api/users/updatePFP/route.ts index e2e27ce..9641323 100644 --- a/src/app/api/users/updatePFP/route.ts +++ b/src/app/api/users/updatePFP/route.ts @@ -1,26 +1,23 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { updateUserPFP } from "~/server/functions"; +import { middleware } from "~/middleware"; interface UpdatePfpRequest { - apiKey: string; userId: number; pfpURL: string; } -export async function POST(request: Request) { +export async function POST(request: NextRequest) { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const { apiKey, userId, pfpURL } = await request.json() as UpdatePfpRequest; - console.log("Received request:", { apiKey, userId, pfpURL }); + const { userId, pfpURL } = await request.json() as UpdatePfpRequest; + console.log("Received request:", { userId, pfpURL }); - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } - - if (!userId || !pfpURL) { + if (!userId || !pfpURL || isNaN(userId)) return NextResponse.json({ message: "Missing required fields" }, { status: 400 }); - } console.log("Updating profile picture for user:", userId); const result = await updateUserPFP(userId, pfpURL); diff --git a/src/app/api/users/updatePushToken/route.ts b/src/app/api/users/updatePushToken/route.ts index 5ace189..cf299de 100644 --- a/src/app/api/users/updatePushToken/route.ts +++ b/src/app/api/users/updatePushToken/route.ts @@ -1,25 +1,27 @@ "use server"; import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { updateUserPushToken } from "~/server/functions"; +import { middleware } from "~/middleware"; interface UpdatePushTokenRequest { - apiKey: string; userId: number; pushToken: string; } -export async function POST(request: Request) { +export async function POST(request: NextRequest) { + const middlewareResponse = await middleware(request); + if (middlewareResponse) return middlewareResponse; try { - const { apiKey, userId, pushToken } = await request.json() as UpdatePushTokenRequest; - console.log("Received request:", { apiKey, userId, pushToken }); - if (apiKey !== process.env.API_KEY) { - console.log("Invalid API Key"); - return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); - } - if (!userId || !pushToken) + const { userId, pushToken } = await request.json() as UpdatePushTokenRequest; + console.log("Received request:", { userId, pushToken }); + + if (!userId || !pushToken || isNaN(userId)) return NextResponse.json({ message: "Missing required fields" }, { status: 400 }); + console.log("Updating push token for user:", userId); const result = await updateUserPushToken(userId, pushToken); + if (result.success) { console.log("Push token updated successfully"); return NextResponse.json({ message: "Push token updated successfully" }); diff --git a/src/app/layout.tsx b/src/app/layout.tsx old mode 100755 new mode 100644 diff --git a/src/app/page.tsx b/src/app/page.tsx old mode 100755 new mode 100644 diff --git a/src/env.js b/src/env.js old mode 100755 new mode 100644 diff --git a/src/middleware.ts b/src/middleware.ts index ebeded1..721a5cb 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,11 +1,20 @@ -import type { NextRequest } from 'next/server'; import { NextResponse } from 'next/server'; +import type { NextRequest } from 'next/server'; -export function middleware(request: NextRequest) { - const apiKey = request.headers.get('x-api-key'); +export async function middleware(request: NextRequest): Promise { + try { + const apiKey = request.headers.get('x-api-key'); - if (!apiKey || apiKey !== process.env.API_KEY) - return NextResponse.json({ message: 'Invalid API key' }, { status: 401 }); + if (!apiKey || apiKey !== process.env.API_KEY) { + return NextResponse.json({ message: 'Invalid API key' }, { status: 401 }); + } + + // If the API key is valid, we don't return anything, allowing the request to proceed + return undefined; + } catch (error) { + console.error('Middleware error:', error); + return NextResponse.json({ message: 'Internal server error' }, { status: 500 }); + } } export const config = { diff --git a/src/server/db/index.ts b/src/server/db/index.ts old mode 100755 new mode 100644 diff --git a/src/server/db/schema.ts b/src/server/db/schema.ts old mode 100755 new mode 100644 diff --git a/src/server/functions.ts b/src/server/functions.ts old mode 100755 new mode 100644 diff --git a/src/styles/globals.css b/src/styles/globals.css old mode 100755 new mode 100644 diff --git a/tailwind.config.ts b/tailwind.config.ts old mode 100755 new mode 100644 diff --git a/tsconfig.json b/tsconfig.json old mode 100755 new mode 100644