@@ -3,13 +3,24 @@ import { NEXT_AUTH_CONFIG } from "@/utils/auth";
33import { getServerSession } from "next-auth" ;
44import { NextRequest , NextResponse } from "next/server" ;
55
6+ const loginRequired = process . env . LOGIN_REQUIRED === "true" ;
67const loginRequired = process . env . LOGIN_REQUIRED === "true" ;
78export const maxDuration = 60 ;
89
910export async function POST ( req : NextRequest ) {
1011 let session = null ;
1112 let user = null ;
1213
14+ if ( loginRequired ) {
15+ session = await getServerSession ( NEXT_AUTH_CONFIG ) ;
16+ if ( ! session ) {
17+ return NextResponse . json ( {
18+ error : 'You are not logged in' ,
19+ } , { status : 401 } )
20+ }
21+ let session = null ;
22+ let user = null ;
23+
1324 if ( loginRequired ) {
1425 session = await getServerSession ( NEXT_AUTH_CONFIG ) ;
1526 if ( ! session ) {
@@ -21,7 +32,21 @@ export async function POST(req: NextRequest) {
2132 user = await prisma . user . findUnique ( {
2233 where : { id : session . user . id } ,
2334 } ) ;
35+ user = await prisma . user . findUnique ( {
36+ where : { id : session . user . id } ,
37+ } ) ;
38+
39+ if ( ! user ) {
40+ return NextResponse . json ( { error : "No user found" } , { status : 401 } ) ;
41+ }
42+ }
2443
44+ const { prompt } : { prompt : string } = await req . json ( ) ;
45+ if ( ! prompt || prompt . length < 5 ) {
46+ return NextResponse . json (
47+ { error : "Invalid prompt. It must be at least 5 characters long." } ,
48+ { status : 400 }
49+ ) ;
2550 if ( ! user ) {
2651 return NextResponse . json ( { error : "No user found" } , { status : 401 } ) ;
2752 }
@@ -81,6 +106,14 @@ export async function POST(req: NextRequest) {
81106}
82107
83108export async function GET ( ) {
109+ if ( loginRequired ) {
110+ const session = await getServerSession ( NEXT_AUTH_CONFIG ) ;
111+ if ( ! session ) {
112+ return NextResponse . json (
113+ { error : "You are Unauthorized" } ,
114+ { status : 401 }
115+ ) ;
116+ }
84117 if ( loginRequired ) {
85118 const session = await getServerSession ( NEXT_AUTH_CONFIG ) ;
86119 if ( ! session ) {
@@ -90,16 +123,30 @@ export async function GET() {
90123 ) ;
91124 }
92125
126+ const user = await prisma . user . findUnique ( {
127+ where : {
128+ id : session . user . id ,
129+ } ,
130+ } ) ;
93131 const user = await prisma . user . findUnique ( {
94132 where : {
95133 id : session . user . id ,
96134 } ,
97135 } ) ;
98136
137+ if ( ! user ) {
138+ return NextResponse . json ( { error : "No user found" } , { status : 401 } ) ;
139+ }
99140 if ( ! user ) {
100141 return NextResponse . json ( { error : "No user found" } , { status : 401 } ) ;
101142 }
102143
144+ const posts = await prisma . post . findMany ( {
145+ where : {
146+ userId : user . id ,
147+ } ,
148+ orderBy : { createdAt : "desc" } ,
149+ } ) ;
103150 const posts = await prisma . post . findMany ( {
104151 where : {
105152 userId : user . id ,
@@ -109,4 +156,6 @@ export async function GET() {
109156
110157 return NextResponse . json ( posts ) ;
111158 }
159+ return NextResponse . json ( posts ) ;
160+ }
112161}
0 commit comments