Next.js Discord

Discord Forum

Next-Auth started to create duplicated session

Answered
Forest yellowjacket posted this in #help-forum
Open in Discord
Forest yellowjacketOP
Hi. I noticed that my application after successfull signIn creates two sessions (screen). It didn't happen before. Where could be a problem? If I have two sessions and I send fetch request to my backend, it sends two requests. I have one layout for whole app :
import { Inter } from "next/font/google";
import "../styles/globals.css";
import "../styles/Main.scss";
import Head from "./head";
import NextAuthSessionProvider from "./SessionProvider";

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

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <Head />
      <body className={inter.className}>
        <NextAuthSessionProvider>
          {children}
        </NextAuthSessionProvider>
      </body>
    </html>
  );
}
and SessionProvider looks like this:
'use client'

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

interface Props {
    children?: React.ReactNode;
    session?: any;
}

export default function NextAuthSessionProvider({children, session}: Props) {
    return (
        <SessionProvider session={session} refetchInterval={0} refetchOnWindowFocus={false}>
            {children}
        </SessionProvider>
    )
}
Answered by Cape horse mackerel
try to set reactStrictMode: false within next.config and see if you're getting it twice, if not, then that's the default behavior when reactStrictMode is set to true
View full answer

19 Replies

Forest yellowjacketOP
Small update: It looks like that every fetch is called twice
Cape horse mackerel
Which version of NextJS are you using?
Cape horse mackerel
try to set reactStrictMode: false within next.config and see if you're getting it twice, if not, then that's the default behavior when reactStrictMode is set to true
Answer
@Cape horse mackerel Which version of NextJS are you using?
Forest yellowjacketOP
I had 13.5.7 and now I upgraded to 14.0.1
Cape horse mackerel
did you upgrade next-auth also?
@Cape horse mackerel did you upgrade `next-auth` also?
Forest yellowjacketOP
yes, to 4.24.4
Cape horse mackerel
I believe that you have to use next-auth@4.24.4 latest version
then try the solution that I wrote you above
@Cape horse mackerel then try the solution that I wrote you above
Forest yellowjacketOP
Yes! It helps. Now the behavior is as I expected. Huge thanks bro 🙂
Cape horse mackerel
yeah, but you don't want to leave reactStrictMode: false
Forest yellowjacketOP
in production?
Cape horse mackerel
that's only happening locally, when you deploy app it will be only one call to the session
Forest yellowjacketOP
ah, good to know
Forest yellowjacketOP
then in production I set it to true
Cape horse mackerel
if answer satisfies you, you can right click => apps => mark as resolved
Forest yellowjacketOP
Strange, I can't see Apps and Mark Solution. (I tried Ctrl + R)
Cape horse mackerel
right click on one of the messages, then you should see it
@Cape horse mackerel right click on one of the messages, then you should see it
Forest yellowjacketOP
Ah, I see now. Thanks 🙂