1- import { GetServerSideProps } from 'next' ;
2- import { parseCookies } from 'nookies' ;
1+ "use client"
32import HeaderItems from "@/app/components/Header/webHeader/headerItems" ;
43import Link from "next/link" ;
5- import React from "react" ;
4+ import React , { useEffect , useState } from "react" ;
65import IButton from "@/app/components/Common/Button" ;
76import { UserProfileResponseInterface } from "@/store/userProfile/interface" ;
87import getUserProfileService from "@/app/services/getUserProfile.service" ;
98
10- // SSR function to check login status
11- export const getServerSideProps : GetServerSideProps = async ( ctx ) => {
12- const cookies = parseCookies ( ctx ) ;
13- const token = cookies [ 'auth-token' ] ; // Get the auth-token from cookies
14-
15- if ( ! token ) {
16- // If the user is not logged in, redirect to login page
17- return {
18- redirect : {
19- destination : '/login' ,
20- permanent : false ,
21- } ,
22- } ;
23- }
9+ export default function Header ( ) {
2410
25- // Fetch user profile if token exists
26- const userProfile = await getUserProfileService ( false ) ;
11+ const [ userProfile , setUserProfile ] = useState < UserProfileResponseInterface > ( ) ;
2712
28- return {
29- props : { userProfile } ,
30- } ;
31- }
13+ const isLogin = localStorage . getItem ( 'isLogin' ) ;
14+
15+ useEffect ( ( ) => {
16+
17+ if ( isLogin ) {
18+ getUserProfileService ( false ) . then ( ( res : any ) => {
19+ setUserProfile ( res )
20+ } )
21+ }
22+ } , [ ] ) ;
3223
33- export default function Header ( { userProfile } : { userProfile : UserProfileResponseInterface } ) {
3424 return (
3525 < div className = { "fixed w-full z-50" } >
3626 < div className = { "flex justify-between items-center h-18 bg-white px-16" } >
37- < Link href = { "/" } className = { 'h-full flex' } >
27+ < Link href = { "/" } className = { 'h-full flex' } >
3828 < img src = { "/safellm.svg" } alt = { "SafeLLM" } />
29+
3930 </ Link >
4031 < HeaderItems />
41- { userProfile ?
32+ { isLogin ?
4233 < div className = { 'flex gap-6 items-center justify-center' } >
4334 < Link href = { "/profile" } >
4435 < h5 > Profile</ h5 >
@@ -62,4 +53,5 @@ export default function Header({ userProfile }: { userProfile: UserProfileRespon
6253 </ div >
6354 </ div >
6455 ) ;
65- }
56+
57+ }
0 commit comments