Next.js Discord

Discord Forum

I want button data not load until session data is retrieved

Unanswered
Rebon posted this in #help-forum
Open in Discord
I have a button in my navbar. If user is not authenticated, it shows "login/resister" and on click redirect to login page, and if the user is authentiacted to have a dropdown menu. The problem I am facing is upon load or refresh "login/resister" is shown for a few milliseconds and if pressed redirects to the login page until the session is retrieved.
"use client"
import PersonIcon from '@mui/icons-material/Person';
import { useSession } from 'next-auth/react';
import { useState } from 'react';
import Dropdown from './DropdownMenu';
import { useRouter } from 'next/navigation'

function Profile () {
  const {data:session} = useSession();
  const router = useRouter()
  const [isDropdown, setDropdown] = useState(false)


  function handleDropdown(){
    setDropdown((prev)=>!prev)
  }

  return (
    <div onClick={session? handleDropdown : ()=>router.push("/signin") }>
      <div className='icon-link'><PersonIcon/>
      <span>{!session? "Login/Regester":""}</span>
      </div>
      {isDropdown&&<Dropdown/>}
    </div>
  )
}

export default Profile

19 Replies

Shy Albatross
useSession() returns an object containing two values: data and status:

    data: This can be three values: Session / undefined / null.
        when the session hasn't been fetched yet, data will be undefined
        in case it failed to retrieve the session, data will be null
        in case of success, data will be Session.
    status: enum mapping to three possible session states: "loading" | "authenticated" | "unauthenticated"
so you probably want something like
<span>{session === undefined ? "Login/Regester" : ""}</span>
or use the status field
@Shy Albatross so you probably want something like `<span>{session === undefined ? "Login/Regester" : ""}</span>`
on the server side it appears that the session is indeed undefined, but when it reaches the client side it is either session or null. so the "Login/Registers" still appear
Shy Albatross
use the status then
@Shy Albatross use the `status` then
I tried. on the server-side it log loading but on the client side it is either "authenticated" | "unauthenticated"
Shy Albatross
idk the docs keep talking about pages router, is it compatible with app router and RSC?
I made the button not make any action until the session is retrieved. if you have another idea on how to achieve it better i would be thrilled
Shy Albatross
well you'd think, but I can't find anything specific about this
v4 dosc are all about pages router
v5 - no docs, auth.js - no docs
so they say you don't need it, this: https://medium.com/@rohitkumarkhatri/next-auth-in-app-router-of-next-js-7df037f7a2ad says you should do what you're doing, but it's not working
well that is awfully strange that they don't have docs for it. did they drop the support or something?
Shy Albatross
I don't know, I used next-auth a lot but only up to Next 12, I'm really disappointed that it's so hard to fin specific info, I looked at both pages at github issues, I spent quite some time now but to no avail
v5 also doesn't seem to be solving more problems than it introduces https://discord.com/channels/752553802359505017/1163034174811275385/1168756844148297812
I guess i would downgrade to v4 cause I can't see that much docs or anything about it