11"use client" ;
2- import { useEffect , useRef , useState } from "react" ;
32import Link from "next/link" ;
43import {
54 DropdownMenu ,
@@ -11,20 +10,24 @@ import {
1110} from "@/components/ui/dropdown-menu" ;
1211import { Button } from "@/components/ui/button" ;
1312import { Avatar , AvatarImage , AvatarFallback } from "@/components/ui/avatar" ;
14- type NavigationProps = {
15- isSignedIn : boolean ;
16- User ?: {
17- name : string ;
18- email : string ;
19- avatar ?: string ;
20- profileHref ?:string ;
21- coursesHref ?:string ;
22- historyHref ?: string ;
23- } ;
24- } ;
13+ import { authClient } from "@/lib/auth-client" ;
14+ const { useSession, signOut } = authClient ;
2515
16+ export default function Navigation ( ) {
17+ const { data : session } = useSession ( ) ;
18+ const user = session ?. user ;
19+ const isSignedIn = Boolean ( user ) ;
20+ const avatarSrc = user ?. image ?? "/user.png" ;
21+ const displayName = user ?. name ?? "User" ;
22+ const email = user ?. email ?? "" ;
2623
27- export default function Navigation ( { isSignedIn, User } : NavigationProps ) {
24+ const handleSignOut = async ( ) => {
25+ try {
26+ await signOut ( ) ;
27+ } catch ( error ) {
28+ console . error ( "Failed to sign out" , error ) ;
29+ }
30+ } ;
2831
2932 return (
3033 < div className = "p-5" >
@@ -56,51 +59,51 @@ export default function Navigation({ isSignedIn, User }: NavigationProps) {
5659 </ Link >
5760 </ Button >
5861 </ li >
59- { isSignedIn && User ? (
62+ { isSignedIn && user ? (
6063 < >
6164
6265 < li >
63- < Button className = "text-lg px-4 py-2 border border-border rounded-md transition-all duration-300 hover:bg-muted hover:text-foreground" >
64- < Link
65- href = "/logout"
66+ < Button
67+ className = "text-lg px-4 py-2 border border-border rounded-md transition-all duration-300 hover:bg-muted hover:text-foreground"
68+ onClick = { handleSignOut }
6669 >
6770 Logout
68- </ Link >
6971 </ Button >
7072 </ li >
7173 { /* Profile Pic Placeholder */ }
7274 < li >
7375 < DropdownMenu >
7476 < DropdownMenuTrigger asChild >
7577 < Avatar className = "w-12 h-12 border-2 border-white rounded-full overflow-hidden transition-all duration-300 hover:border-blue-300 hover:drop-shadow-[1px_1px_40px_rgba(255,215,100,1)] cursor-pointer" >
76- < AvatarImage
77- src = { `/${ User . avatar ?? "user.png" } ` }
78- alt = { User . name }
78+ < AvatarImage
79+ src = { avatarSrc }
80+ alt = { displayName }
81+
7982 className = "object-cover w-full h-full"
8083 />
81- < AvatarFallback > { User . name ?. charAt ( 0 ) . toUpperCase ( ) ?? "?" } </ AvatarFallback >
84+ < AvatarFallback > { displayName . charAt ( 0 ) . toUpperCase ( ) } </ AvatarFallback >
8285 </ Avatar >
8386 </ DropdownMenuTrigger >
8487 < DropdownMenuContent align = "end" className = "w-56" >
8588 < DropdownMenuLabel >
86- < p className = "font-semibold" > { User . name } </ p >
87- < p className = "text-sm text-gray-500" > { User . email } </ p >
89+ < p className = "font-semibold" > { displayName } </ p >
90+ { email && < p className = "text-sm text-gray-500" > { email } </ p > }
8891 </ DropdownMenuLabel >
8992 < DropdownMenuSeparator />
9093 < DropdownMenuItem asChild >
91- < Link href = { User . profileHref ?? "/profile" } > Profile</ Link >
94+ < Link href = "/profile" > Profile</ Link >
9295 </ DropdownMenuItem >
9396 < DropdownMenuItem asChild >
94- < Link href = { User . coursesHref ?? "/courses" } > My Courses</ Link >
97+ < Link href = "/courses" > My Courses</ Link >
9598 </ DropdownMenuItem >
9699 < DropdownMenuItem asChild >
97- < Link href = { User . historyHref ?? "/history" } > History</ Link >
100+ < Link href = "/history" > History</ Link >
98101 </ DropdownMenuItem >
99102 < DropdownMenuSeparator />
100103 < DropdownMenuItem asChild >
101- < Link href = "/logout" className = "text-red-600" >
104+ < button className = "text-red-600" onClick = { handleSignOut } >
102105 Logout
103- </ Link >
106+ </ button >
104107 </ DropdownMenuItem >
105108 </ DropdownMenuContent >
106109 </ DropdownMenu >
@@ -113,7 +116,7 @@ export default function Navigation({ isSignedIn, User }: NavigationProps) {
113116 < Link
114117 href = "/sign-in"
115118 >
116- Login
119+ Sign in
117120 </ Link >
118121 </ Button >
119122
@@ -135,9 +138,9 @@ export default function Navigation({ isSignedIn, User }: NavigationProps) {
135138 < DropdownMenuItem asChild className = "" >
136139 < Button className = "px-4 py-2 border border-border rounded-md transition-all duration-300 hover:bg-muted hover:text-foreground" >
137140 < Link
138- href = "/login "
141+ href = "/sign-in "
139142 >
140- Login
143+ Sign in
141144 </ Link >
142145 </ Button >
143146 </ DropdownMenuItem >
0 commit comments