Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

useSession() hook returning undefined in NextJS v13.4.3 #7839

Closed
Cartrogen opened this issue Jun 19, 2023 · 0 comments
Closed

useSession() hook returning undefined in NextJS v13.4.3 #7839

Cartrogen opened this issue Jun 19, 2023 · 0 comments
Labels
question Ask how to do something or how something works

Comments

@Cartrogen
Copy link

Cartrogen commented Jun 19, 2023

Question 馃挰

I am new to NextJS and I am using v13.4.3. My backend is performing auth and upon login, it is returning a JWT as HTTPONLY cookie which is attached to the browser. My requirement is to build components on the frontend that have conditional buttons/text based on whether the user is logged in or not.

My understanding is that I can use the useSession hook in my components to grab the session from the cookie when the page is rendering and this should tell me whether the user is logged in or not. The problem is that the useSession hook is always returning undefined.

How to reproduce 鈽曪笍

Here is my code and structure. I am using the app structure since I am on the newer version of Next as June 2023.

In my app folder, I have created a folder called auth which has a file called auth-context.tsx - src/app/auth/auth-context.tsx

'use client';

import { SessionProvider } from 'next-auth/react';

export interface AuthContextProps {
  children: React.ReactNode;
}

export default function AuthContext({ children }: AuthContextProps) {
  return <SessionProvider>{children}</SessionProvider>;
}

In my app folder in the main layout file - src/app/layout.tsx

import '@styles/global.css';
import AuthContext from './auth/auth-context';

export const metadata = {
  title: 'myTitle',
  description:
    'myDescription',
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}): JSX.Element {
  return (
    <AuthContext>
      <html lang="en">
        <body>
          <main>{children}</main>
        </body>
      </html>
    </AuthContext>
  );
}

I have done the above in order to wrap the useSession in a sessionProvider by creating a separate client as mentioned by NextJS documentation. I followed this thread to learn about this - #5647

Now the component that needs access to the session is sitting in the (dashboard) folder in app folder - src/app/(dashboard)/home/page.tsx

'use client';

import { useSession } from 'next-auth/react';

export default function Home() {
  const { data } = useSession();
  console.log('LINE7', data);

  return (
    <div>
      HOME
      <p>{data?.user?.name}</p>
      <p>{data?.user?.email}</p>
    </div>
  );
}

The problem I am facing is that the object data always comes back as undefined when I navigate to the home page. When I check the browser, I am seeing the cookie being passed correctly. I decoded the cookie on jwt.io and it is the right cookie that my backend is passing. So it seems like my login functionality is working fine but I am unable to grab the session.

The console in the browser logs this - LINE7 null The console in vs code logs this - LINE7 undefined

Contributing 馃檶馃徑

Yes, I am willing to help answer this question in a PR

@Cartrogen Cartrogen added the question Ask how to do something or how something works label Jun 19, 2023
@nextauthjs nextauthjs locked and limited conversation to collaborators Jun 20, 2023
@balazsorban44 balazsorban44 converted this issue into discussion #7845 Jun 20, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Ask how to do something or how something works
Projects
None yet
Development

No branches or pull requests

1 participant