Next.js Discord

Discord Forum

Is "session" repeating in network tab normal?

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I get like 10 of these in 30 secs
Is this normal?

33 Replies

Asiatic LionOP
Btw im using nextjs
@Asiatic Lion I get like 10 of these in 30 secs Is this normal?
have you added a dependency array to useEffect
@Anay-208 | Ping on replies have you added a dependency array to `useEffect`
Asiatic LionOP
  useEffect(() => {
    if (session?.accessToken) {
      const fetchUserInfo = async () => {
        try {
          const response = await getUserInfo(session.accessToken)
          const data = await response.data          
          setFirstName(data.first_name || '')
          setLastName(data.last_name || '')
          setEmail(data.email || 'Not set')
          setPhone(data.phone || '')
        } catch (err) {
          console.error('Failed to fetch user info:', err)
          setError('Failed to fetch user info')
        }
      }

      fetchUserInfo()
    }
  }, [session?.accessToken]) 
I have multiple useEffects btw
@Anay-208 | Ping on replies accessToken might be getting updated from time to time
Asiatic LionOP
So should I be concerned about it or just let it stay?
By the way a new session request comes when I switch between pages or tabs or have any interaction with the website
They don't come on their own when I don't touch the screen or switching between apps
I'd be concerned if that happens
which file is this, like page or layout
@Anay-208 | Ping on replies which file is this, like page or layout
Asiatic LionOP
app/dashboard/page.tsx
and all the sub directories of it
@Asiatic Lion app/dashboard/page.tsx
so in each page.tsx?
Asiatic LionOP
The session have this data :
{
    "user": {
        "email": null,
        "id": 27,
        "phone": "09021234567",
        "firstName": null,
        "lastName": null,
        "role": "customer",
        "isAdmin": false
    },
    "expires": "2024-12-26T21:21:41.621Z",
    "accessToken": "",
    "refreshToken": ""
}
@Anay-208 | Ping on replies so in each `page.tsx`?
Asiatic LionOP
Only where it's needed
But I see it on all the protected pages
Oh wait sorry
When I'm logged in
I get that session in all of the pages
Everywhere
Even unprotected parts
Why don't you get the session in server action instead?
Asiatic LionOP
And my main layout.tsx is wrapped in SessionProvider btw
    <html lang="en" dir="ltr">
      <body className={`${Diroz.className} antialiased`}>
        <Providers>
          <Suspense fallback={<Loading />}>
            <Navbar />
            {children}
            <Footer />
          </Suspense>
        </Providers>
      </body>
    </html>
are you using any authentication package?
Asiatic LionOP
And the providers.tsx :
'use client'

import { SessionProvider } from "next-auth/react"

export const Providers = ({ children }: { children: React.ReactNode }) => {
  return (
    <SessionProvider>
      {children}
    </SessionProvider>
  )
}
@Anay-208 | Ping on replies are you using any authentication package?
Asiatic LionOP
Yeah nextauth
@Anay-208 | Ping on replies Why don't you get the session in server action instead?
Asiatic LionOP
Like how? it requires the app to be wrapped inside SessionProvider
And I can't put that directly on layout.tsx I need to create another file which is providers.tsx