Fix watch tracking, video unlocking and access-control gaps
Deploy Vault CMS / deploy (push) Failing after 38s
Deploy Vault CMS / deploy (push) Failing after 38s
Watch progress and unlocking: - Player samples the playhead and reports the ranges actually played, so skipped sections earn no credit and early plays are not lost - Server merges ranges into compact segment rows; progress never regresses - Single 80% unlock rule enforced server-side (lib/watch-progress.ts); the Unlock button and automatic unlock now agree - Lock state and progress bar update live from each progress save - Playlist is derived from the video, so unlocks work for shared playlists and for links from watch history and liked videos - First/previous video decided by playlist position, not index numbers Access control: - Video API and HLS/MP4 routes require a signed-in viewer who is enrolled and has the video unlocked; thumbnails require sign-in - Enrolment sync reads course codes from the allow-list, not the request - /api/admin/meta is admin-only - Account status, role and levels are re-checked from the database during a session, so deactivated students lose access within a minute Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import { NextResponse } from "next/server";
|
||||
import { prisma } from "../../../../lib/prisma";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "../../../../lib/auth-options";
|
||||
import { filterAccessibleVideos } from "../../../../lib/watch-progress";
|
||||
|
||||
export async function GET(req: Request, context: any) {
|
||||
try {
|
||||
@@ -20,6 +21,7 @@ export async function GET(req: Request, context: any) {
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email: session.user.email },
|
||||
include: { enrollments: { select: { courseId: true } } },
|
||||
});
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
@@ -37,21 +39,31 @@ export async function GET(req: Request, context: any) {
|
||||
image: true,
|
||||
},
|
||||
},
|
||||
videoCourses: { select: { courseId: true, exclusive: true } },
|
||||
},
|
||||
orderBy: { index: "asc" },
|
||||
},
|
||||
course: true,
|
||||
courses: { select: { courseId: true } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!playlist) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
const enrolled = await prisma.enrollment.findUnique({
|
||||
where: { userId_courseId: { userId: user.id, courseId: playlist.courseId } },
|
||||
});
|
||||
if (!enrolled) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
// Enrolment in the primary course or any course the playlist is shared with.
|
||||
const userCourseIds = user.enrollments.map((e) => e.courseId);
|
||||
const playlistCourseIds = [playlist.courseId, ...playlist.courses.map((c) => c.courseId)];
|
||||
if (!playlistCourseIds.some((courseId) => userCourseIds.includes(courseId))) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ playlist });
|
||||
// Hide videos reserved for courses this user is not in, as /api/playlists does.
|
||||
const visible = {
|
||||
...playlist,
|
||||
videos: filterAccessibleVideos(playlist.videos, userCourseIds),
|
||||
};
|
||||
|
||||
return NextResponse.json({ playlist: visible });
|
||||
} catch (err: any) {
|
||||
console.error("GET /api/playlists/[id] error:", err);
|
||||
return NextResponse.json({ error: "Server error" }, { status: 500 });
|
||||
|
||||
Reference in New Issue
Block a user