nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
23.79k stars 3.27k forks source link

Usage of auth() on client components (v5) #7981

Closed pschaub closed 1 year ago

pschaub commented 1 year ago

Warning: At the time of this issue, next-auth v5 has not yet been released. This issue relates to a pre-release of the upcoming next-auth v5.

What is the improvement or update you wish to see?

I have seen this comment: https://github.com/nextauthjs/next-auth/blob/790f7617083d055b94d05c5c815749258da84852/apps/examples/nextjs/app/page.tsx#L05-L15

Then I have seen the client component: https://github.com/nextauthjs/next-auth/blob/790f7617083d055b94d05c5c815749258da84852/apps/examples/nextjs/app/client.tsx#L5-L7

Afterward, I read this documentation: https://auth-docs-git-feat-nextjs-auth-authjs.vercel.app/guides/upgrade-to-v5#authenticating-server-side

The above code uses auth() on the server component and useSession() on the client component. But the upgrade documentation explains that I should use auth() on both as universal solution.

What is the correct solution? Is the above example outdated?

Is there any context that might help us understand?

See example: https://github.com/nextauthjs/next-auth/tree/790f7617083d055b94d05c5c815749258da84852/apps/examples/nextjs

Does the docs page already exist? Please link to it.

No response

johannbuscail commented 1 year ago

have you found the answer ?

balazsorban44 commented 1 year ago

auth() is meant for server-side, as explained on the page you linked to. For client components and pages/ you can keep using useSession

pschaub commented 1 year ago

@balazsorban44 so auth() should never be used on client-side? Will it throw a warning or error to ensure this?

I was confused because of this line in the upgrade guide:

Universal auth(). Remember a single method, and authenticate anywhere. Replaces getServerSession, getSession, withAuth, getToken and useSession in most cases.

Thank you very much!