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:
@@ -15,8 +15,10 @@ export default async function CourseLayout({ children, params }: Props) {
|
|||||||
const session = await auth.api.getSession({ headers: await headers() });
|
const session = await auth.api.getSession({ headers: await headers() });
|
||||||
if (!session) redirect("/login");
|
if (!session) redirect("/login");
|
||||||
|
|
||||||
|
const isAdmin = session.user.role === "admin";
|
||||||
|
|
||||||
const course = await prisma.course.findUnique({
|
const course = await prisma.course.findUnique({
|
||||||
where: { slug, published: true },
|
where: { slug, ...(isAdmin ? {} : { published: true }) },
|
||||||
include: {
|
include: {
|
||||||
modules: {
|
modules: {
|
||||||
orderBy: { order: "asc" },
|
orderBy: { order: "asc" },
|
||||||
@@ -33,6 +35,7 @@ export default async function CourseLayout({ children, params }: Props) {
|
|||||||
|
|
||||||
if (!course) notFound();
|
if (!course) notFound();
|
||||||
|
|
||||||
|
if (!isAdmin) {
|
||||||
const enrollment = await prisma.courseEnrollment.findUnique({
|
const enrollment = await prisma.courseEnrollment.findUnique({
|
||||||
where: { userId_courseId: { userId: session.user.id, courseId: course.id } },
|
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()) {
|
if (enrollment.expiresAt && enrollment.expiresAt < new Date()) {
|
||||||
redirect("/dashboard?expired=1");
|
redirect("/dashboard?expired=1");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-1">
|
<div className="flex flex-1">
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { prisma } from "@/lib/prisma";
|
import { prisma } from "@/lib/prisma";
|
||||||
import { notFound } from "next/navigation";
|
import { notFound } from "next/navigation";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
import { headers } from "next/headers";
|
||||||
|
import { auth } from "@/lib/auth";
|
||||||
import { KinescopePlayer } from "@/components/player/kinescope-player";
|
import { KinescopePlayer } from "@/components/player/kinescope-player";
|
||||||
import { LessonContent } from "@/components/student/lesson-content";
|
import { LessonContent } from "@/components/student/lesson-content";
|
||||||
|
|
||||||
@@ -11,8 +13,11 @@ interface Props {
|
|||||||
export default async function LessonPage({ params }: Props) {
|
export default async function LessonPage({ params }: Props) {
|
||||||
const { slug, lessonId } = await params;
|
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({
|
const lesson = await prisma.lesson.findUnique({
|
||||||
where: { id: lessonId, published: true },
|
where: { id: lessonId, ...(isAdmin ? {} : { published: true }) },
|
||||||
include: {
|
include: {
|
||||||
files: { orderBy: { createdAt: "asc" } },
|
files: { orderBy: { createdAt: "asc" } },
|
||||||
module: {
|
module: {
|
||||||
|
|||||||
Reference in New Issue
Block a user