@@ -3,6 +3,47 @@ import connectDB from '@/lib/db';
33import { DSOCApplication } from '@/models/DSOCApplication' ;
44import { DSOCProject } from '@/models/DSOCProject' ;
55import { DSOCMentee } from '@/models/DSOCMentee' ;
6+ import jwt from 'jsonwebtoken' ;
7+
8+ async function getMentorFromToken ( request : NextRequest ) {
9+ const token = request . cookies . get ( 'dsoc-mentor-token' ) ?. value ;
10+ if ( ! token ) return null ;
11+
12+ try {
13+ const decoded = jwt . verify ( token , process . env . JWT_SECRET as string ) as { id : string ; role : string } ;
14+ if ( decoded . role !== 'dsoc-mentor' ) return null ;
15+ return decoded . id ;
16+ } catch {
17+ return null ;
18+ }
19+ }
20+
21+ async function getAdminFromToken ( request : NextRequest ) {
22+ const token = request . cookies . get ( 'admin-token' ) ?. value ;
23+ if ( ! token ) return null ;
24+
25+ try {
26+ const decoded = jwt . verify ( token , process . env . JWT_SECRET as string ) as { id ?: string } ;
27+ return decoded . id || null ;
28+ } catch {
29+ return null ;
30+ }
31+ }
32+
33+ function hasMentorAccess (
34+ mentorId : string ,
35+ project : { mentors ?: Array < { _id ?: string } | string > } | null
36+ ) {
37+ if ( ! project || ! Array . isArray ( project . mentors ) ) return false ;
38+
39+ return project . mentors . some ( ( mentor ) => {
40+ if ( ! mentor ) return false ;
41+ if ( typeof mentor === 'string' ) return mentor === mentorId ;
42+ if ( mentor . _id ) return mentor . _id . toString ( ) === mentorId ;
43+ const asAny = mentor as { toString ?: ( ) => string } ;
44+ return asAny . toString ?.( ) === mentorId ;
45+ } ) ;
46+ }
647
748// GET single application
849export async function GET (
@@ -12,6 +53,15 @@ export async function GET(
1253 try {
1354 await connectDB ( ) ;
1455 const { id } = await params ;
56+
57+ const mentorId = await getMentorFromToken ( request ) ;
58+ const adminId = await getAdminFromToken ( request ) ;
59+ if ( ! mentorId && ! adminId ) {
60+ return NextResponse . json (
61+ { success : false , error : 'Unauthorized' } ,
62+ { status : 401 }
63+ ) ;
64+ }
1565
1666 const application = await DSOCApplication . findById ( id )
1767 . populate ( 'project' , 'title organization status mentors' )
@@ -24,6 +74,16 @@ export async function GET(
2474 { status : 404 }
2575 ) ;
2676 }
77+
78+ if ( mentorId ) {
79+ const project = application . project as { mentors ?: Array < { _id ?: string } | string > } | null ;
80+ if ( ! hasMentorAccess ( mentorId , project ) ) {
81+ return NextResponse . json (
82+ { success : false , error : 'Forbidden' } ,
83+ { status : 403 }
84+ ) ;
85+ }
86+ }
2787
2888 return NextResponse . json ( {
2989 success : true ,
@@ -46,8 +106,16 @@ export async function PUT(
46106 try {
47107 await connectDB ( ) ;
48108 const { id } = await params ;
49-
50- // TODO: Add mentor/admin authentication check
109+
110+ const mentorId = await getMentorFromToken ( request ) ;
111+ const adminId = await getAdminFromToken ( request ) ;
112+ if ( ! mentorId && ! adminId ) {
113+ return NextResponse . json (
114+ { success : false , error : 'Unauthorized' } ,
115+ { status : 401 }
116+ ) ;
117+ }
118+
51119 const body = await request . json ( ) ;
52120 const { status, mentorNotes, adminNotes, score } = body ;
53121
@@ -59,6 +127,19 @@ export async function PUT(
59127 { status : 404 }
60128 ) ;
61129 }
130+
131+ if ( mentorId ) {
132+ const project = await DSOCProject . findById ( application . project )
133+ . select ( 'mentors' )
134+ . lean ( ) ;
135+
136+ if ( ! project || ! hasMentorAccess ( mentorId , project ) ) {
137+ return NextResponse . json (
138+ { success : false , error : 'Forbidden' } ,
139+ { status : 403 }
140+ ) ;
141+ }
142+ }
62143
63144 // Update application
64145 if ( status ) application . status = status ;
0 commit comments