Skip to content

Conversation

@thomasballinger
Copy link
Contributor

@thomasballinger thomasballinger commented Aug 29, 2025

app/layout.tsx Outdated
}: Readonly<{
children: React.ReactNode;
}>) {
const expectAuth = !!(await withAuth()).user

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this refactor! Super clear.

One thing to point out is the call to withAuth here causes all children of the layout to be dynamic routes, removing the option for the more performant static routes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point, seems like we should be declaring routes/component trees as expecting auth instead of asking whether the user is authed to decide.

Ideally <Authenticated> could take care of this: <Authenticated> could render its children during SSR and on hydration when authed, or if we can't have that access token available (related issues) then at least know that auth is coming, and we can do a more localized version of expectAuth: true.

@nicknisi any plans to be able to check on the first render whether a route is authed, or more specifically whether an accessToken is coming?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we want grouped routes here so we can use different layouts here, yea? app/(authed)/layout.tsx so it can be server-rendered

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any plans to be able to check on the first render whether a route is authed, or more specifically whether an accessToken is coming?

@thomasballinger wondering if the changes in workos/authkit-nextjs#297 would help to simplify this?

Copy link
Member

@nicknisi nicknisi Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomasballinger After some testing I made some minor tweaks in cd7d9a5 and upgraded to the latest authkit-nextjs in 1dc0978. I pushed them up to this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

const preloaded = await preloadQuery(api.myFunctions.listNumbers, {
count: 3,
});
const { accessToken } = await withAuth();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this call to withAuth essentially instant since we already authenticated in the middleware.ts? Or does this make a server round trip, meaning we have a round trip in middleware followed by a second round trip here (on an already authenticated route)?

If it is the latter, would it make sense for us to switch the middleware.ts to page based auth instead of the current middleware auth? Middleware documentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question! the withAuth() call is essentially instant. It reads the session from a header that the middleware already set. There's no additional authentication or API call happening.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh sweet. Thanks for the info!

@nicknisi nicknisi merged commit 626846e into workos:main Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants