From e0c54941bd959d51ed62e34bd18f835d39a2c250 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Fri, 22 Nov 2024 18:07:03 +0600 Subject: [PATCH] feat: handle auth users --- src/components/Course/CourseView.tsx | 48 ++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/src/components/Course/CourseView.tsx b/src/components/Course/CourseView.tsx index fc0d188c9..1b3b23931 100644 --- a/src/components/Course/CourseView.tsx +++ b/src/components/Course/CourseView.tsx @@ -1,4 +1,4 @@ -import { lazy, type ReactNode } from 'react'; +import { lazy, useEffect, useState, type ReactNode } from 'react'; import type { LessonFileType } from '../../lib/course'; import { CourseLayout, type CourseLayoutProps } from './CourseLayout'; import { @@ -6,6 +6,9 @@ import { ResizablePanel, ResizablePanelGroup, } from '../Resizable'; +import { isLoggedIn } from '../../lib/jwt'; +import { IdCardIcon, Loader2Icon } from 'lucide-react'; +import { showLoginPopup } from '../../lib/popup'; const SqlCodeEditor = lazy(() => import('../SqlCodeEditor/SqlCodeEditor').then((module) => ({ @@ -38,6 +41,45 @@ export function CourseView(props: CourseViewProps) { 'lesson-quiz', ].includes(lessonType); + const [isLoading, setIsLoading] = useState(true); + const isAuthenticated = isLoggedIn(); + + useEffect(() => { + if (isAuthenticated === undefined) { + return; + } + + setIsLoading(false); + }, [isAuthenticated]); + + if (isLoading) { + return ( +
+ +
+ ); + } + + if (!isAuthenticated) { + return ( +
+ +

+ You need to be logged in to access this course. +

+ + +
+ ); + } + return ( @@ -49,10 +91,10 @@ export function CourseView(props: CourseViewProps) {
-

+

{lesson.frontmatter.title}

-
+
{children}