1
1
import { auth } from '@/app/(auth)/auth' ;
2
- import { ArtifactKind } from '@/components/artifact' ;
2
+ import type { ArtifactKind } from '@/components/artifact' ;
3
3
import {
4
4
deleteDocumentsByIdAfterTimestamp ,
5
5
getDocumentsById ,
@@ -45,7 +45,7 @@ export async function POST(request: Request) {
45
45
46
46
const session = await auth ( ) ;
47
47
48
- if ( ! session ) {
48
+ if ( ! session ?. user ?. id ) {
49
49
return new Response ( 'Unauthorized' , { status : 401 } ) ;
50
50
}
51
51
@@ -56,19 +56,25 @@ export async function POST(request: Request) {
56
56
} : { content : string ; title : string ; kind : ArtifactKind } =
57
57
await request . json ( ) ;
58
58
59
- if ( session . user ?. id ) {
60
- const document = await saveDocument ( {
61
- id,
62
- content,
63
- title,
64
- kind,
65
- userId : session . user . id ,
66
- } ) ;
59
+ const documents = await getDocumentsById ( { id : id } ) ;
67
60
68
- return Response . json ( document , { status : 200 } ) ;
61
+ if ( documents . length > 0 ) {
62
+ const [ document ] = documents ;
63
+
64
+ if ( document . userId !== session . user . id ) {
65
+ return new Response ( 'Forbidden' , { status : 403 } ) ;
66
+ }
69
67
}
70
68
71
- return new Response ( 'Unauthorized' , { status : 401 } ) ;
69
+ const document = await saveDocument ( {
70
+ id,
71
+ content,
72
+ title,
73
+ kind,
74
+ userId : session . user . id ,
75
+ } ) ;
76
+
77
+ return Response . json ( document , { status : 200 } ) ;
72
78
}
73
79
74
80
export async function PATCH ( request : Request ) {
0 commit comments