Allow admin to preview unpublished lessons without enrollment

- Course layout skips enrollment and published checks for admin role
- Lesson page skips published filter for admin role
- Enables admin preview button to work for any lesson/course state

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-07 12:57:18 +05:00
parent 4183a912e4
commit c88b5d2004
2 changed files with 17 additions and 8 deletions
+5 -1
View File
@@ -15,8 +15,10 @@ export default async function CourseLayout({ children, params }: Props) {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) redirect("/login");
const isAdmin = session.user.role === "admin";
const course = await prisma.course.findUnique({
where: { slug, published: true },
where: { slug, ...(isAdmin ? {} : { published: true }) },
include: {
modules: {
orderBy: { order: "asc" },
@@ -33,6 +35,7 @@ export default async function CourseLayout({ children, params }: Props) {
if (!course) notFound();
if (!isAdmin) {
const enrollment = await prisma.courseEnrollment.findUnique({
where: { userId_courseId: { userId: session.user.id, courseId: course.id } },
});
@@ -42,6 +45,7 @@ export default async function CourseLayout({ children, params }: Props) {
if (enrollment.expiresAt && enrollment.expiresAt < new Date()) {
redirect("/dashboard?expired=1");
}
}
return (
<div className="flex flex-1">
@@ -1,6 +1,8 @@
import { prisma } from "@/lib/prisma";
import { notFound } from "next/navigation";
import Link from "next/link";
import { headers } from "next/headers";
import { auth } from "@/lib/auth";
import { KinescopePlayer } from "@/components/player/kinescope-player";
import { LessonContent } from "@/components/student/lesson-content";
@@ -11,8 +13,11 @@ interface Props {
export default async function LessonPage({ params }: Props) {
const { slug, lessonId } = await params;
const session = await auth.api.getSession({ headers: await headers() });
const isAdmin = session?.user.role === "admin";
const lesson = await prisma.lesson.findUnique({
where: { id: lessonId, published: true },
where: { id: lessonId, ...(isAdmin ? {} : { published: true }) },
include: {
files: { orderBy: { createdAt: "asc" } },
module: {