Skip to content

Commit

Permalink
fix(react): allow imports from "next-auth/react" in RSC (#5718)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenflow authored and balazsorban44 committed Nov 5, 2022
1 parent 2f3396d commit f498e9c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/next-auth/src/react/index.tsx
Expand Up @@ -95,7 +95,7 @@ export type SessionContextValue<R extends boolean = false> = R extends true
| { data: Session; status: "authenticated" }
| { data: null; status: "unauthenticated" | "loading" }

export const SessionContext = React.createContext<
export const SessionContext = React.createContext?.<
SessionContextValue | undefined
>(undefined)

Expand All @@ -106,6 +106,10 @@ export const SessionContext = React.createContext<
* [Documentation](https://next-auth.js.org/getting-started/client#usesession)
*/
export function useSession<R extends boolean>(options?: UseSessionOptions<R>) {
if (!SessionContext) {
throw new Error("React Context is unavailable in Server Components")
}

// @ts-expect-error Satisfy TS if branch on line below
const value: SessionContextValue<R> = React.useContext(SessionContext)
if (!value && process.env.NODE_ENV !== "production") {
Expand Down Expand Up @@ -322,6 +326,10 @@ export async function signOut<R extends boolean = true>(
* [Documentation](https://next-auth.js.org/getting-started/client#sessionprovider)
*/
export function SessionProvider(props: SessionProviderProps) {
if (!SessionContext) {
throw new Error("React Context is unavailable in Server Components")
}

const { children, basePath, refetchInterval, refetchWhenOffline } = props

if (basePath) __NEXTAUTH.basePath = basePath
Expand Down

0 comments on commit f498e9c

Please sign in to comment.