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({