next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

[next-auth]: `useSession` must be wrapped in a <SessionProvider /> pages folder move to inside src folder-

Open manoj-mukherjee-maersk opened this issue 5 years ago • 21 comments

Description 🐜

Moving pages folder to src. Getting error [next-auth]: useSession must be wrapped in a . Working fine when pages folder on root dir.

Screenshot 2021-10-29 at 1 43 21 PM

Is this a bug in your own project?

No

How to reproduce ☕️

Move pages folder to inside src folder mkdir src move pages to src dir

Screenshots / Logs 📽

PFA - Error Screenshot 2021-10-29 at 1 41 07 PM

Environment 🖥

System: OS: macOS 11.3.1 CPU: (8) arm64 Apple M1 Memory: 128.27 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 16.8.0 - /opt/homebrew/bin/node Yarn: 1.22.11 - /opt/homebrew/bin/yarn npm: 7.21.0 - /opt/homebrew/bin/npm Browsers: Chrome: 95.0.4638.54 Edge: 92.0.902.67 Safari: 14.1 npmPackages: next: 12.0.1 => 12.0.1 next-auth: ^4.0.0-beta.5 => 4.0.0-beta.5 react: 17.0.2 => 17.0.2

Contributing 🙌🏽

No, I am afraid I cannot help regarding this

manoj-mukherjee-maersk avatar Oct 29 '21 08:10 manoj-mukherjee-maersk

could you please share your _app.tsx file?

balazsorban44 avatar Oct 29 '21 08:10 balazsorban44

I am having same issue, this is my _app.js:

import '@/css/tailwind.css'
import '@/css/prism.css'
import { SessionProvider } from "next-auth/react"
import { ThemeProvider } from 'next-themes'
import Head from 'next/head'

import Analytics from '@/components/analytics'
import LayoutWrapper from '@/components/LayoutWrapper'
import { ClientReload } from '@/components/ClientReload'
import { SWRConfig } from 'swr'
import axios from 'axios'
import AuthGuard from '../components/AuthGuard'

const isDevelopment = process.env.NODE_ENV === 'development'

export default function App({
  Component,
  pageProps: { session, ...pageProps },
}) {
  return (
    <ThemeProvider attribute="class">
      <Head>
        <meta content="width=device-width, initial-scale=1" name="viewport" />
      </Head>
      {isDevelopment && <ClientReload />}
      <Analytics />
      <LayoutWrapper>
        
      <SessionProvider session={session}>
          <SWRConfig
            value={{
              fetcher: (url) => axios.get(url).then((res) => res.data),
            }}
          >
            <Component {...pageProps} />
          </SWRConfig>
    </SessionProvider>
      </LayoutWrapper>
    </ThemeProvider>
  )
}

vahidtakro avatar Oct 29 '21 22:10 vahidtakro

are you using useSession in your ThemeProvider, LayoutWrapper or Analytics components?

I also see some potentially unused imports like ClientReload and AuthGuard. what about those?

balazsorban44 avatar Oct 29 '21 23:10 balazsorban44

are you using useSession in your ThemeProvider, LayoutWrapper or Analytics components?

I also see some potentially unused imports like ClientReload and AuthGuard. what about those?

I just changed my _app.js to this:

import '@/css/tailwind.css'
import '@/css/prism.css'
import { SessionProvider } from "next-auth/react"
import LayoutWrapper from '@/components/LayoutWrapper'

export default function App({
  Component,
  pageProps: { session, ...pageProps },
}) {
  return (

      <LayoutWrapper>
      <SessionProvider session={session}>
            <Component {...pageProps} />
    </SessionProvider>
      </LayoutWrapper>

  )
}

still same error, I am using useSession in my LayoutWrapper component.

the error appears wherever (in any components) I import this: import { signIn, signOut, useSession } from 'next-auth/react'

and use this before return: const { data: session, status } = useSession()

vahidtakro avatar Oct 30 '21 08:10 vahidtakro

Then that's the issue. Your SessionProvider has to come before LayoutWrapper.

balazsorban44 avatar Oct 30 '21 09:10 balazsorban44

Then that's the issue. Your SessionProvider has to come before LayoutWrapper.

Thanks! the problem solved !

vahidtakro avatar Oct 30 '21 10:10 vahidtakro

@manoj-mukherjee-maersk without seeing your code, I am almost certain you have the same issue described over. make sure that anything using useSession is under SessionProvider.

I'm going to close this for now, feel free to reach out again if the problem isn't resolved with the above.

balazsorban44 avatar Oct 30 '21 10:10 balazsorban44

In my case, the problem was related to cache. I deleted the .next folder and worked.

DonHuskini avatar Feb 18 '22 14:02 DonHuskini

@gabrielmlinassi - almost 1 year later, you saved some of my hair-pulling. I believe I had an incorrect cache that had not been invalidated, after switching between TS and JS; I had not even thought of manually emptying this cache.

JasonAllenCorns avatar Feb 02 '23 00:02 JasonAllenCorns

In my case, the problem was related to cache. I deleted the .next folder and worked.

you just saved me hours of frustration... thank you!

BrandonThomas84 avatar Feb 16 '23 18:02 BrandonThomas84

i'm having this issue with nextjs 13 gone through series of ways to fix it but still the same

mosgizy avatar May 07 '23 00:05 mosgizy

I'm having the same issue with nextjs 13. The error appear in the server side but the client side looks good and working.

thadeubrito avatar May 20 '23 19:05 thadeubrito

For someone that use nextjs 13. CC @mosgizy @thadeubrito Mine is working after make a new SessionProvider components like this:

'use client';
import React from 'react'

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

type Props = {}

const SessionProvider = ({children}: {children:React.ReactNode}) => {
  return (
    <AuthSessionProvider>{children}</AuthSessionProvider>
  )
}

export default SessionProvider

And then wrap the most outer layout or root layout that usually placed in here /src/app/layout.tsx using that component like this:

import "./globals.css";
import { Inter } from "next/font/google";
import SessionProvider from "@/components/SessionProvider";

const inter = Inter({ subsets: ["latin"] });

export const metadata = {
  title: "Create Next App",
  description: "Generated by create next app",
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body className={`${inter.className}`}>
        <SessionProvider>
          {children}
        </SessionProvider>
      </body>
    </html>
  );
}

nayakayoga avatar May 25 '23 01:05 nayakayoga

Unfortunately I'm having this same problem and have done exactly what @nayakayoga suggested and it's still telling me I'm using useSession outside the SessionProvider - but I'm only using useSession once and it's in a component on the main page rendered directly inside the SessionProvider

Update: it works if you put the SessionProvider in the page.tsx file - though that means something is broken with the way layout works.

DaveMadden avatar Jun 02 '23 22:06 DaveMadden

@nayakayoga Thanks for the solution ! This issue got me searching for a few hours... Out of curiosity, how did you find this solution ?

vegandiet705 avatar Jul 17 '23 20:07 vegandiet705

got the same issue, this helped me to solve it. Thanks @nayakayoga for your explicit solution...

tagnefabrice2020 avatar Nov 10 '23 13:11 tagnefabrice2020

i'm with this same issue when i try to run tests with the vitest, i try some soluctions here but nothing works.

_app.tsx:

import type { AppProps } from 'next/app';
import { QueryClientProvider, QueryClient } from 'react-query'
import { ReactQueryDevtools } from 'react-query/devtools'
import '@/styles/globals.css';

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

const queryClient = new QueryClient()

export default function App({
  Component,
  pageProps: { session, ...pageProps },
}: AppProps) {
  return (
    <SessionProvider session={session}>
      <QueryClientProvider client={queryClient}>
        <Component {...pageProps} />
        <ReactQueryDevtools initialIsOpen={false} />
      </QueryClientProvider>
    </SessionProvider>
  )

}

and here is the output in my test terminal:


 FAIL  src/__tests__/index.test.tsx > Home
Error: [next-auth]: `useSession` must be wrapped in a <SessionProvider />
 ❯ Proxy.useSession node_modules/next-auth/react/index.js:119:11
 ❯ Home src/pages/index.tsx:8:27
      6| 
      7| export default function Home() {
      8|   const {data: session} = useSession();        
       |                           ^
      9|   if (session?.user) {
     10|     return (
     
     ``` 

IsmaferFerramentas avatar Dec 26 '23 13:12 IsmaferFerramentas

I am using next14 and unable to solve this error

⨯ node_modules\next-auth\react\index.js (113:10) @ useSession ⨯ Error: React Context is unavailable in Server Components at Home (./app/page.tsx:20:90) at stringify () digest: "938141719" null

/page.tsx/

import Documents from "@/components/Documents"; import Header from "@/components/Header"; import Hero from "@/components/Hero"; import Login from "@/components/Login"; import { useSession, getSession } from "next-auth/react";

export default function Home() { const { data: session } = useSession(); if (!session) return ;

return (

); }

/Layout.tsx/

import type { Metadata } from "next"; import { Inter } from "next/font/google"; import "./globals.css";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = { title: "Google Docs", description: "Generated by create next app", };

export default function RootLayout({ children, }: { children: React.ReactNode; }) { return ( {children} ); }

Abhishek058 avatar Jan 10 '24 10:01 Abhishek058

For someone that use nextjs 13. CC @mosgizy @thadeubrito Mine is working after make a new SessionProvider components like this:

'use client';
import React from 'react'

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

type Props = {}

const SessionProvider = ({children}: {children:React.ReactNode}) => {
  return (
    <AuthSessionProvider>{children}</AuthSessionProvider>
  )
}

export default SessionProvider

And then wrap the most outer layout or root layout that usually placed in here /src/app/layout.tsx using that component like this:

import "./globals.css";
import { Inter } from "next/font/google";
import SessionProvider from "@/components/SessionProvider";

const inter = Inter({ subsets: ["latin"] });

export const metadata = {
  title: "Create Next App",
  description: "Generated by create next app",
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body className={`${inter.className}`}>
        <SessionProvider>
          {children}
        </SessionProvider>
      </body>
    </html>
  );
}

its working for me

BhushanPatil-121 avatar Jan 31 '24 08:01 BhushanPatil-121

Its working for me but, I am getting below error: "Application error: a client-side exception has occurred (see the browser console for more information).". After refreshing website its working fine. There is some rendering warning : "Warning: Cannot update a component (HotReload) while rendering a different component (Router). To locate the bad setState() call inside Router, follow the stack trace as described in https://reactjs.org/link/setstate-in-render"

Nemidas7734 avatar Feb 19 '24 15:02 Nemidas7734