From f7d569cf921a225aa7b77130c7efa0018eb12587 Mon Sep 17 00:00:00 2001 From: Brandon Johnson Date: Thu, 24 Sep 2020 14:54:24 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#48140=20[next-auth?= =?UTF-8?q?]=20Allow=20session=20to=20be=20null=20or=20undefined=20by=20@b?= =?UTF-8?q?john465?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While the session data is being fetched, `useSession` will return `undefined`. If a session doesn't exist, `useSession` will return `null`. The result of `useSession` is commonly passed to the `Provider` component (e.g. see the examples at https://next-auth.js.org/getting-started/client#provider), and it seems to allow the `undefined` and `null` values for the `session` prop, so allow them in the type information, too. --- types/next-auth/client.d.ts | 4 ++-- types/next-auth/next-auth-tests.ts | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/types/next-auth/client.d.ts b/types/next-auth/client.d.ts index 2d71939b5b..c87c0d469f 100644 --- a/types/next-auth/client.d.ts +++ b/types/next-auth/client.d.ts @@ -25,7 +25,7 @@ interface SessionProvider extends GenericObject { } interface ContextProviderProps { - session: Session; + session: Session | null | undefined; options?: SetOptionsParams; } @@ -43,7 +43,7 @@ interface NextContext { ctx?: { req: IncomingMessage }; } -declare function useSession(): [Session, boolean]; +declare function useSession(): [Session | null | undefined, boolean]; declare function providers(): Promise; declare const getProviders: typeof providers; declare function session( diff --git a/types/next-auth/next-auth-tests.ts b/types/next-auth/next-auth-tests.ts index 9cc127b3e2..d05774089d 100644 --- a/types/next-auth/next-auth-tests.ts +++ b/types/next-auth/next-auth-tests.ts @@ -190,7 +190,7 @@ const session = { expires: '1234', }; -// $ExpectType [Session, boolean] +// $ExpectType [Session | null | undefined, boolean] client.useSession(); // $ExpectType Promise @@ -230,6 +230,28 @@ client.Provider({ }, }); +// $ExpectType ReactElement | null +client.Provider({ + session, +}); + +// $ExpectType ReactElement | null +client.Provider({ + session: undefined, + options: {}, +}); + +// $ExpectType ReactElement | null +client.Provider({ + session: null, + options: { + baseUrl: 'https://foo.com', + basePath: '/', + clientMaxAge: 1234, + keepAlive: 4321, + }, +}); + // -------------------------------------------------------------------------- // Providers // --------------------------------------------------------------------------