From c285cdc397dd2e1ab66a0cfc31db56ffe668be35 Mon Sep 17 00:00:00 2001 From: YASH <139299779+Yash-2707@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:04:00 +0530 Subject: [PATCH] Update page.tsx Added error handling for the fetchUser function to log any errors that occur during the user fetch process. Added a handleLogout function that signs the user out and redirects them to the login page. Added a loading state to display a spinner while fetching user details. This improves the user experience by providing feedback that something is happening. UI Enhancements --- frontend/app/protected/page.tsx | 61 +++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/frontend/app/protected/page.tsx b/frontend/app/protected/page.tsx index 147361d..aaca9c4 100644 --- a/frontend/app/protected/page.tsx +++ b/frontend/app/protected/page.tsx @@ -1,13 +1,54 @@ import { createClient } from "@/utils/supabase/server"; -import { InfoIcon } from "lucide-react"; +import { InfoIcon, LogOutIcon, Loader2 } from "lucide-react"; import { redirect } from "next/navigation"; +import { useState, useEffect } from "react"; export default async function ProtectedPage() { const supabase = createClient(); + const [user, setUser] = useState(null); + const [loading, setLoading] = useState(true); - const { - data: { user }, - } = await supabase.auth.getUser(); + useEffect(() => { + async function fetchUser() { + const { + data: { user }, + error + } = await supabase.auth.getUser(); + + if (error) { + console.error("Error fetching user:", error); + setLoading(false); + return; + } + + if (!user) { + redirect("/login"); + } else { + setUser(user); + setLoading(false); + } + } + + fetchUser(); + }, [supabase]); + + const handleLogout = async () => { + const { error } = await supabase.auth.signOut(); + if (error) { + console.error("Error logging out:", error); + } else { + redirect("/login"); + } + }; + + if (loading) { + return ( +
+ +

Loading user details...

+
+ ); + } if (!user) { return redirect("/login"); @@ -18,8 +59,7 @@ export default async function ProtectedPage() {
- This is a protected page that you can only see as an authenticated - user + This is a protected page that you can only see as an authenticated user
@@ -28,6 +68,15 @@ export default async function ProtectedPage() { {JSON.stringify(user, null, 2)}
+
+ +

Next steps