Next.js Discord

Discord Forum

Do I need to run auth() twice (in middleware to authorize and in Server component to get username?

Unanswered
Cuckoo wasp posted this in #help-forum
Open in Discord
Cuckoo waspOP
As the title says, I have to run nextauth 5, auth() function to authorize the user to some paths. In one page inside the path, I have to access the username of the user. So, I would end up calling the auth() function twice. Is there any better way?

11 Replies

Cuckoo waspOP
I figured I would just set the header to carry the username.
Middleware is not part of the caching mechanism handled by React.cache.

I use layout files to handle protected pages now: all protected pages have one common layout where auth() is run. Then you can use React.cache to dedupe the auth() run in the page and the layout.
@joulev Middleware is not part of the caching mechanism handled by React.cache. I use layout files to handle protected pages now: all protected pages have one common layout where auth() is run. Then you can use React.cache to dedupe the auth() run in the page and the layout.
Cuckoo waspOP
If I cache the auth() function, how would I revalidate it when a user logs out? Even after logging out, won't the user get authorised to the protected routes due to cache?
@Cuckoo wasp If I cache the auth() function, how would I revalidate it when a user logs out? Even after logging out, won't the user get authorised to the protected routes due to cache?
React.cache only dedupes all duplicated runs of a function during a single render, so it’s all good
When you log out and switch page, it’s a separate render
@joulev React.cache only dedupes all duplicated runs of a function during a single render, so it’s all good
Cuckoo waspOP
Didn't know that. I thought it stores the data until the revalidation. I was using the cache function for a database query in a server component
@Cuckoo wasp Didn't know that. I thought it stores the data until the revalidation. I was using the cache function for a database query in a server component
That one is the cache mechanism handled by unstable_cache (data cache and full route cache)

React.cache only does the request memoisation cache

(Yes it’s super confusing)
@joulev That one is the cache mechanism handled by unstable_cache (data cache and full route cache) React.cache only does the request memoisation cache (Yes it’s super confusing)
Cuckoo waspOP
Kinda get your point. Since you said cache() only works for a single render, does it mean if I reload the page, the database query will run again?
@Ray yes, you should use `unstable_cache` for caching the database query instead
Cuckoo waspOP
I am confused. So, I read here (https://nextjs.org/docs/app/building-your-application/caching#react-cache-function) that I can surround the db query with cache function to memoize it. But @joulev says react.cache only memoizes function during the single render. If it is so then why are they suggesting to surround the db query with React.cache? Is there different way how React.cache() works in server components? I'm sorry, I'm new to nextjs.