From 62f4f4f35fb3529dabbe85f235dee3d59024222f Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 2 Mar 2025 22:06:30 +0100 Subject: [PATCH] Fixed the cookie issue (i think) --- package.json | 2 +- src/app/api/account/route.ts | 1 - src/app/api/database.ts | 4 +- src/app/api/env.ts | 3 +- src/app/api/posts/route.ts | 67 ++++++++++-------- src/app/api/session/route.ts | 25 ++++--- src/app/calendar/calendar.tsx | 7 -- src/app/calendar/page.tsx | 23 ++++--- src/app/calendar/postDialog.tsx | 32 ++++++--- src/app/calendar/timeSelect.tsx | 1 - src/app/dashboard/engagement.tsx | 113 ++++++++++++++++--------------- src/app/dashboard/page.tsx | 42 ++++++------ src/app/login/page.tsx | 14 ++-- src/app/page.tsx | 11 +-- src/utils.ts | 2 - 15 files changed, 184 insertions(+), 163 deletions(-) diff --git a/package.json b/package.json index 3b8e1d6..47c9fc4 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.1-alpha.0", "private": true, "scripts": { - "dev": "next dev", + "dev": "clear && next dev", "build": "next build", "start": "next start", "lint": "next lint", diff --git a/src/app/api/account/route.ts b/src/app/api/account/route.ts index 06531c7..bb855b9 100644 --- a/src/app/api/account/route.ts +++ b/src/app/api/account/route.ts @@ -44,7 +44,6 @@ export async function POST(req: NextRequest) { secure: process.env.NODE_ENV === 'production', sameSite: 'strict', path: '/', - maxAge: 60 * 60 * 24, // 1 day }); return response; diff --git a/src/app/api/database.ts b/src/app/api/database.ts index 393ceb5..f3a623c 100644 --- a/src/app/api/database.ts +++ b/src/app/api/database.ts @@ -1,9 +1,11 @@ import { MongoClient } from 'mongodb'; import config from './env'; -import { Schedule, ScheduleRequest, Session, User } from '@/utils'; +import { Schedule, Session, User } from '@/utils'; const client = new MongoClient(config.mongoUrl); +await client.connect(); + const db = client.db('klayrion'); export default { diff --git a/src/app/api/env.ts b/src/app/api/env.ts index 7b2f2ff..fecb9cb 100644 --- a/src/app/api/env.ts +++ b/src/app/api/env.ts @@ -8,7 +8,8 @@ const config = { secret: process.env.TWITTER_SECRET || error('TWITTER_SECRET'), }, - mongoUrl: process.env.MONGO_URL || error('MONGO_URL'), + mongoUrl: + 'mongodb+srv://admin:foAyEvJcTbR50Dlv@cluster0.gj9zm.mongodb.net/', }; export default config; diff --git a/src/app/api/posts/route.ts b/src/app/api/posts/route.ts index 3275a6a..061f160 100644 --- a/src/app/api/posts/route.ts +++ b/src/app/api/posts/route.ts @@ -6,20 +6,20 @@ import { ObjectId } from 'mongodb'; import { HttpStatusCode } from 'axios'; export async function POST(req: NextRequest) { - const { sessionId, content, platforms }: PostRequest = await req.json(); + const sessionId = req.cookies.get('session_id'); - console.log(sessionId); - - if (typeof sessionId !== 'string') + if (!sessionId) return NextResponse.json( { - message: 'Invalid structure', + message: 'Invalid session', }, - { status: 400 } + { status: 404 } ); + const { content, platforms }: PostRequest = await req.json(); + const session = await database.sessions.findOne({ - _id: new ObjectId(sessionId), + _id: new ObjectId(sessionId.value), }); if (!session) @@ -45,14 +45,19 @@ export async function POST(req: NextRequest) { } export async function PUT(req: NextRequest) { - const { sessionId, scheduled, content, platforms }: ScheduleRequest = - await req.json(); + const sessionId = req.cookies.get('session_id'); - if ( - typeof sessionId !== 'string' || - typeof scheduled !== 'string' || - typeof content !== 'string' - ) + if (!sessionId) + return NextResponse.json( + { + message: 'Invalid session', + }, + { status: HttpStatusCode.NotFound } + ); + + const { scheduled, content, platforms }: ScheduleRequest = await req.json(); + + if (typeof scheduled !== 'string' || typeof content !== 'string') return NextResponse.json( { message: 'Invalid structure', @@ -61,7 +66,7 @@ export async function PUT(req: NextRequest) { ); const session = await database.sessions.findOne({ - _id: new ObjectId(sessionId), + _id: new ObjectId(sessionId.value), }); if (!session) @@ -89,18 +94,18 @@ export async function PUT(req: NextRequest) { } export async function GET(req: NextRequest) { - const sessionId = new URL(req.url).searchParams.get('id'); + const sessionId = req.cookies.get('session_id'); - if (typeof sessionId !== 'string') + if (!sessionId) return NextResponse.json( { - message: 'Invalid structure, required param `id=`', + message: 'Invalid session', }, - { status: HttpStatusCode.BadRequest } + { status: HttpStatusCode.NotFound } ); const session = await database.sessions.findOne({ - _id: new ObjectId(sessionId), + _id: new ObjectId(sessionId.value), }); if (!session) @@ -127,21 +132,27 @@ export async function GET(req: NextRequest) { } export async function DELETE(req: NextRequest) { - const url = new URL(req.url); - const sessionId = url.searchParams.get('id'); - const schedule = url.searchParams.get('schedule'); - - if (typeof sessionId !== 'string' || typeof schedule !== 'string') + const sessionId = req.cookies.get('session_id'); + if (!sessionId) return NextResponse.json( { - message: - 'Invalid structure, required param `id=` and `schedule=`', + message: 'Invalid session', + }, + { status: 404 } + ); + + const schedule = new URL(req.url).searchParams.get('schedule'); + + if (typeof schedule !== 'string') + return NextResponse.json( + { + message: 'Invalid structure', }, { status: HttpStatusCode.BadRequest } ); const session = await database.sessions.findOne({ - _id: new ObjectId(sessionId), + _id: new ObjectId(sessionId.value), }); if (!session) diff --git a/src/app/api/session/route.ts b/src/app/api/session/route.ts index e1ad37c..29b2f82 100644 --- a/src/app/api/session/route.ts +++ b/src/app/api/session/route.ts @@ -2,22 +2,21 @@ import { NextResponse, NextRequest } from 'next/server'; import database from '../database'; import { ObjectId } from 'mongodb'; -// Account login -// Email exists -// Password is valid export async function GET(req: NextRequest) { - const sessionId = new URL(req.url).searchParams.get('id'); + const sessionId = req.cookies.get('session_id'); - if (typeof sessionId !== 'string') + console.log(sessionId); + + if (!sessionId) return NextResponse.json( { - message: 'Invalid structure, required param `id=`', + message: 'Session not found', }, - { status: 400 } + { status: 401 } ); const user = await database.sessions.findOne({ - _id: new ObjectId(sessionId), + _id: new ObjectId(sessionId.value), }); if (!user) @@ -25,7 +24,7 @@ export async function GET(req: NextRequest) { { message: 'Invalid session', }, - { status: 404 } + { status: 401 } ); return NextResponse.json( @@ -88,10 +87,10 @@ export async function POST(req: NextRequest) { ); response.cookies.set('session_id', sessionId.toString(), { - httpOnly: true, - secure: process.env.NODE_ENV === 'production', - sameSite: 'strict', - path: '/', + httpOnly: false, + secure: false, + sameSite: 'lax', + path: '/api', maxAge: 60 * 60 * 24, // 1 day }); diff --git a/src/app/calendar/calendar.tsx b/src/app/calendar/calendar.tsx index 340e2d6..8ea1c39 100644 --- a/src/app/calendar/calendar.tsx +++ b/src/app/calendar/calendar.tsx @@ -43,14 +43,12 @@ const times: [string, number][] = [ export default function Calendar({ setDialog, - sessionId, events, setDate, date, setEvents, }: { setDialog: (b: boolean) => void; - sessionId: string; events: CalendarSchedule[]; date: Date; setDate: (d: Date) => void; @@ -86,10 +84,6 @@ export default function Calendar({
{times.map((t, j) => { const event = events.find((e) => { - console.log( - e.date.getHours(), - t[1] - ); return ( e.date.getFullYear() === w[1] && e.date.getMonth() === w[2] && @@ -143,7 +137,6 @@ export default function Calendar({ `/api/posts`, { params: { - id: sessionId, schedule: event.id, } satisfies ScheduleRemoveRequest, diff --git a/src/app/calendar/page.tsx b/src/app/calendar/page.tsx index 0d23597..023109e 100644 --- a/src/app/calendar/page.tsx +++ b/src/app/calendar/page.tsx @@ -15,15 +15,19 @@ export default function CalendarApp() { const [events, setEvents] = useState([]); useEffect(() => { - axios.get(`/api/posts/?id=${sessionId}`).then((response) => { - const v = response.data.schedules.map((e: any) => ({ - date: parse(e.scheduled, 'yyyy-MM-dd HH:mm', new Date()), - platforms: e.platforms, - id: e._id, - })); - setEvents(v); - setEventCount(response.data.schedules.length); - }); + axios + .get(`/api/posts/?id=${sessionId}`, { + withCredentials: true, + }) + .then((response) => { + const v = response.data.schedules.map((e: any) => ({ + date: parse(e.scheduled, 'yyyy-MM-dd HH:mm', new Date()), + platforms: e.platforms, + id: e._id, + })); + setEvents(v); + setEventCount(response.data.schedules.length); + }); }, []); const [postDialog, setDialog] = useState(false); @@ -47,7 +51,6 @@ export default function CalendarApp() { { - axios.post('/api/posts', { - sessionId, - platforms: ['andjksds'], - content, - } satisfies PostRequest); + axios.post( + '/api/posts', + { + platforms: ['andjksds'], + content, + } satisfies PostRequest, + { + withCredentials: true, + } + ); setNewPostDialogOpen(false); }} @@ -139,12 +144,17 @@ export default function PostDialog({