Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): support importing "next-auth/react" in Server Component environment #5718

Conversation

zenflow
Copy link
Contributor

@zenflow zenflow commented Nov 4, 2022

☕️ Reasoning

Currently, importing "next-auth/react" in a Server Component module (or in a module imported by a Server Component module) will fail with Uncaught TypeError: React.createContext is not a function.

This is because React Context is unavailable in Server Components.

With this change, "next-auth/react" can be imported without error, and an informative error is thrown if there's an attempt to actually use React Context.

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

Related issue: #5647 (this PR doesn't add complete support, but improves support)

Currently, importing "next-auth/react" in a Server Component module (or in a module imported by a Server Component module) will fail with `Uncaught TypeError: React.createContext is not a function`.

This is because React Context is unavailable in Server Components.

With this change, "next-auth/react" can be imported without error, and an informative error is thrown if there's an attempt to actually *use* React Context.
@zenflow zenflow requested a review from ThangHuuVu as a November 4, 2022 02:31
@vercel
Copy link

vercel bot commented Nov 4, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
next-auth ⬜️ Ignored (Inspect) Nov 4, 2022 at 2:32AM (UTC)

@github-actions github-actions bot added client Client related code core Refers to `@auth/core` labels Nov 4, 2022
@zenflow zenflow changed the title Support importing "next-auth/react" in Server Component environment feat: support importing "next-auth/react" in Server Component environment Nov 4, 2022
@balazsorban44
Copy link
Member

Thanks, we plan to design a better API for the Next.js app directory, but as for now, the recommendation is to add the 'use client' directive to the file where you wrap your components with SessionProvider.

@zenflow
Copy link
Contributor Author

zenflow commented Nov 4, 2022

as for now, the recommendation is to add the 'use client' directive to the file where you wrap your components with SessionProvider.

That's basically what I'm doing...

"use client";
// This module exists for the sole purpose of cloning SessionProvider & adding the compiler directive above

import React from "react";
import { SessionProvider } from "next-auth/react";

export const ClientSessionProvider: typeof SessionProvider = (props) => {
  return <SessionProvider {...props} />;
};

... but it doesn't solve this separate problem (described under Reasoning above).

we plan to design a better API for the Next.js app directory

@balazsorban44 Since that will probably take some time, would you consider merging this small change as a stop-gap?

@balazsorban44 balazsorban44 reopened this Nov 5, 2022
@balazsorban44
Copy link
Member

balazsorban44 commented Nov 5, 2022

Upon revisiting, I think this is a good solution for now and does not introduce problems, even if we want to change APIs in the future. Given app/ is experimental in Next.js, we are also free to do breaking changes if needed.

@balazsorban44 balazsorban44 changed the title feat: support importing "next-auth/react" in Server Component environment fix(react): support importing "next-auth/react" in Server Component environment Nov 5, 2022
@balazsorban44 balazsorban44 merged this pull request into nextauthjs:main Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Client related code core Refers to `@auth/core`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants