How to handle getSession delay on client components with NextAuth?

Hey all. So with next auth, there seems to be a delay every time the getSession hook is used to make a request, which isn’t a big deal but can be a bit cumbersome when conditionally performing actions based on login state. Wondering what people are doing to get around this, I’m considering using server components as well as storing the session object in global state and revalidating whenever necessary. Any other thoughts on how to handle this?
27 Replies
bakdaddy
bakdaddy12mo ago
why not use a hook? useSession? as you are doing it client side I think it may be easier
dylz1
dylz112mo ago
@bakdaddy i'm using the useSession hook, but there's a ~1 second delay before the data is retrieved
bakdaddy
bakdaddy12mo ago
Do you use prisma adapter? Maybe the db call is the bottleneck
dylz1
dylz112mo ago
@bakdaddy funny enough, i just tried switching to JWT and it seems to eliminate the slow down. Almost all of my components are client side so makes much more sense to store the session locally with JWT thanks for the help, marking resolved
bakdaddy
bakdaddy12mo ago
Yeah, that seems to be the bottleneck, maybe dbadapter is too slow
dylz1
dylz112mo ago
Well i'm not running my db locally, so i think its just a latency thing
bakdaddy
bakdaddy12mo ago
Jwt strategy doesn't involve DBs, so there you go
dylz1
dylz112mo ago
:)
bakdaddy
bakdaddy12mo ago
HMU I you have more questions, happy to help
dylz1
dylz112mo ago
@bakdaddy So even though it is much quicker, the session is still undefined on initial render. Here's the code export default function ResultsPage() { const { data: sessionData } = useSession() console.log(sessionData) return 'test' }
bakdaddy
bakdaddy12mo ago
Yeah, it's cause it can be not authenticated Do like this useSession({required: true})
dylz1
dylz112mo ago
the session is being fetched correctly, just undefined initially
bakdaddy
bakdaddy12mo ago
Or you can conditionally do something like if(!session) return <p>Not authorised</p>
dylz1
dylz112mo ago
bakdaddy
bakdaddy12mo ago
Try passing required true object to useSession as a parameter
dylz1
dylz112mo ago
still no luck
bakdaddy
bakdaddy12mo ago
Sorry, using mobile so slow typing Let me get on my laptop, one sec
dylz1
dylz112mo ago
no worries even if i wrap in a useEffect, still gonna be undefined initially
bakdaddy
bakdaddy12mo ago
Don't, hooks usually don't go inside useeffect
dylz1
dylz112mo ago
i just mean console logging the value from the hook not the actual hook
bakdaddy
bakdaddy12mo ago
Sure, got it, almost booted my laptop Dang it Discord is updating...
dylz1
dylz112mo ago
Is this expected behavior? I can always just store user data in global state, but kinda defeats the purpose of the useSession hook
dylz1
dylz112mo ago
so looks like the status is loading when its undefined
dylz1
dylz112mo ago
seems to always be loading state initially before it is authenticated, even when using JWT
bakdaddy
bakdaddy12mo ago
I think its because it's getting/loading the session either try const {session} = useSession({required: true}) or const {session} = useSession(); if (!session) return <p>Loading session...</p> console.log(session) 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" thats from docs
dylz1
dylz112mo ago
yeah i think this is expected behavior then
Want results from more Discord servers?
Add your server
More Posts
[SOLVED] Next.js App Router with Express Custom ServerHi Theo, I watched your YouTube video on App Router Review six months in, great video, thanks for poDisable some eslint rules this in extend pluginsHello ! I have this eslint config : ```ts // eslint-disable-next-line @typescript-eslint/no-var-Do I belong here?Is this Discord server for members of a TypeScript course? Or is it for anyone interested in TypescrProtect routes? For the app itself, no user auth, pages router. Help, desperate.Hi, I'm a noob and I've been winging it with Chat GPT My app is ready, Im using next js 13, now I TypeError: Cannot read properties of undefined (reading 'subscribe')Hi, I have the following code and I keep getting the error "Cannot read properties of undefined (reatRPC query gets the type right on the way in and wrong on the way out?This is a weird one. I have a public procedure (query) that takes a string as an input. Part of the Why does the official Twitter website look so different on mobile?So I recently completed theo's Twitter clone tutorial and wanted to go a bit further make the projecRequest and update as the server comes alongSo, I have a scenario in which I have a list of currencies, like `["USD", "EUR", "BRL"]` etc, that hCan't resolve 'fs' but in an API route???Any idea why this error is occuring? I thought all api routes are node compatible... ``` Module not React Query cache is not working?I started a new t3-turbo-app project then added refetchInterval: 1000 in the post router to update