|
1 | | -import { headers } from "next/headers"; |
2 | | -import { redirect } from "next/navigation"; |
3 | | -import { auth } from "@/lib/auth"; |
| 1 | +"use client"; |
4 | 2 |
|
5 | | -export default async function DashboardPage() { |
6 | | - const session = await auth.api.getSession({ |
7 | | - headers: await headers(), |
8 | | - }); |
| 3 | +import { useRouter } from "next/navigation"; |
| 4 | +import { useEffect, useState } from "react"; |
| 5 | +import { signOut, useSession } from "@/lib/auth-client"; |
9 | 6 |
|
10 | | - if (!session) { |
11 | | - redirect("/login"); |
| 7 | +export default function DashboardPage() { |
| 8 | + const router = useRouter(); |
| 9 | + const { data: session, isPending } = useSession(); |
| 10 | + const [isSigningOut, setIsSigningOut] = useState(false); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + if (!isPending && !session) { |
| 14 | + router.push("/sign-in"); |
| 15 | + } |
| 16 | + }, [session, isPending, router]); |
| 17 | + |
| 18 | + const handleSignOut = async () => { |
| 19 | + setIsSigningOut(true); |
| 20 | + try { |
| 21 | + await signOut(); |
| 22 | + } catch (error) { |
| 23 | + console.error("Sign-out error:", error); |
| 24 | + setIsSigningOut(false); |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + if (isPending || !session) { |
| 29 | + return null; |
12 | 30 | } |
13 | 31 |
|
14 | 32 | return ( |
@@ -36,14 +54,14 @@ export default async function DashboardPage() { |
36 | 54 | </div> |
37 | 55 | </div> |
38 | 56 |
|
39 | | - <form action="/api/auth/sign-out" method="POST"> |
40 | | - <button |
41 | | - type="submit" |
42 | | - className="rounded-full bg-red-600 px-6 py-3 text-white transition-colors hover:bg-red-700" |
43 | | - > |
44 | | - Sign Out |
45 | | - </button> |
46 | | - </form> |
| 57 | + <button |
| 58 | + type="button" |
| 59 | + onClick={handleSignOut} |
| 60 | + disabled={isSigningOut} |
| 61 | + className="rounded-full bg-red-600 px-6 py-3 text-white transition-colors hover:bg-red-700 disabled:opacity-50" |
| 62 | + > |
| 63 | + {isSigningOut ? "Signing Out..." : "Sign Out"} |
| 64 | + </button> |
47 | 65 | </main> |
48 | 66 | </div> |
49 | 67 | ); |
|
0 commit comments