diff --git a/app/browse/page.tsx b/app/browse/page.tsx index 764d393..adb5899 100644 --- a/app/browse/page.tsx +++ b/app/browse/page.tsx @@ -15,6 +15,8 @@ import { loadingVariants, } from "../props/animations"; import BrowseLoader from "./components/BrowseLoader"; +import { useAuth } from "../context/AuthContext"; +import NotLoggedIn from "../../components/globals/NotLoggedIn"; const Browse = () => { const searchParams = useSearchParams(); @@ -29,6 +31,7 @@ const Browse = () => { const [listings, setListings] = useState([]); const searchBarRef = useRef(null); const [loading, setLoading] = useState(true); + const { user, loading: authLoading } = useAuth(); useEffect(() => { const fetchListings = async () => { @@ -82,6 +85,37 @@ const Browse = () => { } }; + // Show loading while auth is being checked + if (authLoading) { + return ( + + + + ); + } + + // Show not logged in component if user is not authenticated + if (!user) { + return ( + + + + ); + } + return ( import("../listing/components/MapPicker"), { ssr: false }); @@ -26,7 +27,7 @@ const MapPicker = dynamic(() => import("../listing/components/MapPicker"), { ssr const Create = () => { const [images, setImages] = useState([]); const fileInputRef = useRef(null); - const { user } = useAuth(); + const { user, loading: authLoading } = useAuth(); const router = useRouter(); const [title, setTitle] = useState(""); const [category, setCategory] = useState(""); @@ -92,7 +93,6 @@ const Create = () => { const handleSaveDraft = async () => { if (!user?.id) { toast.error("You must be logged in to save a draft."); - router.push('/auth/signin'); return; } @@ -136,7 +136,6 @@ const Create = () => { const handleSubmit = async () => { if (!user?.id) { toast.error("You must be logged in to create a listing."); - router.push('/auth/signin'); return; } @@ -179,6 +178,40 @@ const Create = () => { } }; + // Show loading while auth is being checked + if (authLoading) { + return ( + +
+
+

Loading...

+
+
+ ); + } + + // Show not logged in component if user is not authenticated + if (!user) { + return ( + + + + ); + } + return ( { const [editId, setEditId] = useState(null); useEffect(() => { - if (!authLoading && !user) { - router.push("/auth/signin"); - return; - } if (!authLoading && user) { fetchListings(); } @@ -219,6 +216,28 @@ const MyListings = () => { } }; + // Show loading while auth is being checked + if (authLoading) { + return ; + } + + // Show not logged in component if user is not authenticated + if (!user) { + return ( + + + + ); + } + if (loading) { return ; } diff --git a/components/globals/NotLoggedIn.tsx b/components/globals/NotLoggedIn.tsx new file mode 100644 index 0000000..63ba3d5 --- /dev/null +++ b/components/globals/NotLoggedIn.tsx @@ -0,0 +1,65 @@ +"use client"; +import React from 'react'; +import { motion } from 'framer-motion'; +import { useRouter } from 'next/navigation'; +import { LogIn, UserX } from 'lucide-react'; + +interface NotLoggedInProps { + message?: string; + showLoginButton?: boolean; + className?: string; +} + +const NotLoggedIn: React.FC = ({ + message = "You're not logged in", + showLoginButton = true, + className = "" +}) => { + const router = useRouter(); + + const handleLoginClick = () => { + router.push('/auth/signin'); + }; + + return ( + + +
+
+ +
+ +

+ {message} +

+ +

+ Please log in to access this feature and start using UT Marketplace. +

+ + {showLoginButton && ( + + )} +
+
+
+ ); +}; + +export default NotLoggedIn;