diff --git a/front/app/_orgs/[orgslug]/(withmenu)/course/[courseid]/edit/page.tsx b/front/app/_orgs/[orgslug]/(withmenu)/course/[courseid]/edit/page.tsx index afc7840e..45f0d10f 100644 --- a/front/app/_orgs/[orgslug]/(withmenu)/course/[courseid]/edit/page.tsx +++ b/front/app/_orgs/[orgslug]/(withmenu)/course/[courseid]/edit/page.tsx @@ -1,5 +1,6 @@ "use client"; import React from "react"; + import { useState, useEffect } from "react"; import styled from "styled-components"; import { Title } from "@components/UI/Elements/Styles/Title"; @@ -16,6 +17,8 @@ import Modal from "@components/UI/Modal/Modal"; import AuthProvider from "@components/Security/AuthProvider"; function CourseEdit(params: any) { + + const router = useRouter(); // Initial Course State const [data, setData] = useState(initialData2) as any; @@ -31,9 +34,14 @@ function CourseEdit(params: any) { const orgslug = params.params.orgslug; async function getCourseChapters() { - const courseChapters = await getCourseChaptersMetadata(courseid); - setData(courseChapters); - console.log("courseChapters", courseChapters); + try { + const courseChapters = await getCourseChaptersMetadata(courseid); + setData(courseChapters); + } catch (error: any) { + if (error.status === 401) { + router.push("/login"); + } + } } useEffect(() => { @@ -226,7 +234,6 @@ function CourseEdit(params: any) { return ( <> - Edit Course {" "} diff --git a/front/services/courses/chapters.ts b/front/services/courses/chapters.ts index 86ad056f..8bdc85e2 100644 --- a/front/services/courses/chapters.ts +++ b/front/services/courses/chapters.ts @@ -9,10 +9,15 @@ import { RequestBody } from "@services/utils/requests"; //TODO : depreciate this function export async function getCourseChaptersMetadata(course_id: any) { - const data: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null)) - .then((result) => result.json()) - .catch((error) => console.log("error", error)); + const response = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null)); + if (!response.ok) { + const error: any = new Error(`Error ${response.status}: ${response.statusText}`, {}); + error.status = response.status; + throw error; + } + + const data = await response.json(); return data; } @@ -20,7 +25,7 @@ export async function updateChaptersMetadata(course_id: any, data: any) { const result: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("PUT", data)) .then((result) => result.json()) .catch((error) => console.log("error", error)); - + return result; } diff --git a/src/security/auth.py b/src/security/auth.py index c075ab92..fe61fe24 100644 --- a/src/security/auth.py +++ b/src/security/auth.py @@ -90,4 +90,6 @@ async def get_current_user(request: Request, Authorize: AuthJWT = Depends()): else: return AnonymousUser() - \ No newline at end of file +async def non_public_endpoint(current_user: PublicUser ): + if isinstance(current_user, AnonymousUser): + raise HTTPException(status_code=401, detail="Not authenticated") \ No newline at end of file diff --git a/src/services/courses/chapters.py b/src/services/courses/chapters.py index 92b82b71..94cdd195 100644 --- a/src/services/courses/chapters.py +++ b/src/services/courses/chapters.py @@ -4,6 +4,7 @@ import pprint from typing import List from uuid import uuid4 from pydantic import BaseModel +from src.security.auth import non_public_endpoint from src.services.courses.courses import Course, CourseInDB from src.services.courses.activities.activities import Activity, ActivityInDB from src.security.security import verify_user_rights_with_roles @@ -149,10 +150,16 @@ async def get_coursechapters_meta(request: Request, course_id: str, current_user courses = request.app.db["courses"] activities = request.app.db["activities"] + await non_public_endpoint(current_user) + coursechapters = await courses.find_one({"course_id": course_id}, {"chapters": 1, "chapters_content": 1, "_id": 0}) coursechapters = coursechapters + if not coursechapters: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, detail="Course does not exist") + # activities coursechapter_activityIds_global = []