mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
**What**: These changes ensure that we work more tightly with React that can also result in unforeseen performance boosts. In case we would decide on expanding to other libraries/frameworks, a new file per framework could be added. **Why**: Some performance issues (https://github.com/nextauthjs/next-auth/issues/844) could only be fixed by moving more of the client code into the `Provider`. **How**: Refactoring `next-auth/client` Related: #1461, #1084, #1462 BREAKING CHANGE: **1.** `next-auth/client` is renamed to `next-auth/react`. **2.** In the past, we exposed most of the functions with different names for convenience. To simplify our source code, the new React specific client code exports only the following functions, listed with the necessary changes: - `setOptions`: Not exposed anymore, use `SessionProvider` props - `options`: Not exposed anymore, use `SessionProvider` props - `session`: Rename to `getSession` - `providers`: Rename to `getProviders` - `csrfToken`: Rename to `getCsrfToken` - `signin`: Rename to `signIn` - `signout`: Rename to `signOut` - `Provider`: Rename to `SessionProvider` **3.** `Provider` changes. - `Provider` is renamed to `SessionProvider` - The `options` prop is now flattened as the props of `SessionProvider`. - `clientMaxAge` has been renamed to `staleTime`. - `keepAlive` has been renamed to `refetchInterval`. An example of the changes: ```diff - <Provider options={{clientMaxAge: 0, keepAlive: 0}}>{children}</Provider> + <SessionProvider staleTime={0} refetchInterval={0}>{children}</SessionProvider> ``` **4.** It is now **required** to wrap the part of your application that uses `useSession` into a `SessionProvider`. Usually, the best place for this is in your `pages/_app.jsx` file: ```jsx import { SessionProvider } from "next-auth/react" export default function App({ Component, pageProps: { session, ...pageProps } }) { return ( // `session` comes from `getServerSideProps` or `getInitialProps`. // Avoids flickering/session loading on first load. <SessionProvider session={session}> <Component {...pageProps} /> </SessionProvider> ) } ```
84 lines
2.0 KiB
TypeScript
84 lines
2.0 KiB
TypeScript
import * as client from "../react-client"
|
|
import { nextReq } from "./test-helpers"
|
|
|
|
const clientSession = {
|
|
user: {
|
|
name: "Bruce",
|
|
email: "bruce@lee.com",
|
|
image: "path/to/img",
|
|
},
|
|
accessToken: "123z",
|
|
expires: "1234",
|
|
}
|
|
|
|
// $ExpectType [Session | null, boolean]
|
|
client.useSession()
|
|
|
|
// $ExpectType Promise<Session | null>
|
|
client.getSession({ req: nextReq })
|
|
|
|
// $ExpectType Promise<Record<string, ClientSafeProvider> | null>
|
|
client.getProviders()
|
|
|
|
// $ExpectType Promise<string | null>
|
|
client.getCsrfToken({ req: nextReq })
|
|
|
|
// $ExpectType Promise<string | null>
|
|
client.getCsrfToken({ ctx: { req: nextReq } })
|
|
|
|
// $ExpectType Promise<undefined>
|
|
client.signIn("github", { callbackUrl: "foo" }, { login: "username" })
|
|
|
|
// $ExpectType Promise<SignInResponse | undefined>
|
|
client.signIn("credentials", { callbackUrl: "foo", redirect: true })
|
|
|
|
// $ExpectType Promise<SignInResponse | undefined>
|
|
client.signIn("credentials", { redirect: false })
|
|
|
|
// $ExpectType Promise<SignInResponse | undefined>
|
|
client.signIn("email", { callbackUrl: "foo", redirect: false })
|
|
|
|
// $ExpectType Promise<SignInResponse | undefined>
|
|
client.signIn("email", { callbackUrl: "foo", redirect: true })
|
|
|
|
// $ExpectType Promise<undefined>
|
|
client.signOut()
|
|
|
|
// $ExpectType Promise<undefined>
|
|
client.signOut({ callbackUrl: "https://foo.com/callback", redirect: true })
|
|
|
|
// $ExpectType Promise<SignOutResponse>
|
|
client.signOut({ callbackUrl: "https://foo.com/callback", redirect: false })
|
|
|
|
// $ExpectType ReactElement<any, any> | null
|
|
client.SessionProvider({
|
|
children: null,
|
|
session: clientSession,
|
|
baseUrl: "https://foo.com",
|
|
basePath: "/",
|
|
staleTime: 1234,
|
|
})
|
|
|
|
// $ExpectType ReactElement<any, any> | null
|
|
client.SessionProvider({
|
|
children: null,
|
|
session: clientSession,
|
|
})
|
|
|
|
// $ExpectType ReactElement<any, any> | null
|
|
client.SessionProvider({
|
|
children: null,
|
|
})
|
|
|
|
// $ExpectType ReactElement<any, any> | null
|
|
client.SessionProvider({
|
|
children: null,
|
|
session: {
|
|
expires: "",
|
|
},
|
|
baseUrl: "https://foo.com",
|
|
basePath: "/",
|
|
staleTime: 1234,
|
|
refetchInterval: 4321,
|
|
})
|