diff --git a/.changeset/blue-bikes-ring.md b/.changeset/blue-bikes-ring.md new file mode 100644 index 0000000..5d8e27e --- /dev/null +++ b/.changeset/blue-bikes-ring.md @@ -0,0 +1,5 @@ +--- +'keystone-6-oauth': patch +--- + +Types - wip - optimize diff --git a/packages/keystone-6-oauth/src/index.ts b/packages/keystone-6-oauth/src/index.ts index f2432d2..8e80618 100644 --- a/packages/keystone-6-oauth/src/index.ts +++ b/packages/keystone-6-oauth/src/index.ts @@ -1,4 +1,3 @@ -import url from 'url'; import { AdminFileToWrite, BaseListTypeInfo, @@ -8,19 +7,22 @@ import { BaseKeystoneTypeInfo, SessionStrategy, } from '@keystone-6/core/types'; -import { getSession } from 'next-auth/react'; -import { getToken, JWT } from 'next-auth/jwt'; -import { Provider } from 'next-auth/providers'; import * as cookie from 'cookie'; import { Session } from 'next-auth'; +import { getSession } from 'next-auth/react'; +import { getToken, JWT } from 'next-auth/jwt'; +import { Provider } from 'next-auth/providers'; + import { nextConfigTemplate } from './templates/next-config'; -// import * as Path from 'path'; +import { authTemplate } from './templates/auth'; -import { AuthConfig, KeystoneOAuthConfig, AuthSessionStrategy } from './types'; import { getSchemaExtension } from './schema'; -import { authTemplate } from './templates/auth'; + +import { AuthConfig, KeystoneOAuthConfig, AuthSessionStrategy } from './types'; + +import url from 'url'; /** * createAuth function @@ -28,7 +30,14 @@ import { authTemplate } from './templates/auth'; * Generates config for Keystone to implement standard auth features. */ -export type { NextAuthProviders, KeystoneOAuthConfig } from './types'; +export type { + NextAuthProviders, + KeystoneOAuthConfig, + KeystoneOAuthCallbacks, + KeystoneOAuthOnSignIn, + KeystoneOAuthOnSignUp + } from './types'; + export function createAuth({ autoCreate, context, diff --git a/packages/keystone-6-oauth/src/pages/NextAuthPage.tsx b/packages/keystone-6-oauth/src/pages/NextAuthPage.tsx index 9890ecf..f8f2863 100644 --- a/packages/keystone-6-oauth/src/pages/NextAuthPage.tsx +++ b/packages/keystone-6-oauth/src/pages/NextAuthPage.tsx @@ -1,30 +1,9 @@ -/* eslint-disable no-param-reassign */ -import NextAuth, { - CookiesOptions, - EventCallbacks, - PagesOptions, -} from 'next-auth'; -import type { KeystoneContext } from '@keystone-6/core/types'; -import { Provider } from 'next-auth/providers'; -import { JWTOptions } from 'next-auth/jwt'; -import { validateNextAuth } from '../lib/validateNextAuth'; - -import { NextAuthTemplateProps, OAuthCallbacks } from '../types'; +import NextAuth from 'next-auth'; -export type CoreNextAuthPageProps = { - cookies?: Partial; - events?: Partial; - jwt?: Partial; - pages?: Partial; - providers: Provider[]; -} & NextAuthTemplateProps & OAuthCallbacks; - -export type NextAuthPageProps = CoreNextAuthPageProps & { - context: KeystoneContext; -}; +import { validateNextAuth } from '../lib/validateNextAuth'; +import { KeystoneOAuth } from '../types'; -// eslint-disable-next-line react/function-component-definition -export default function NextAuthPage(props: NextAuthPageProps) { +export default function NextAuthPage(props: KeystoneOAuth) { const { autoCreate, cookies, @@ -180,5 +159,5 @@ export default function NextAuthPage(props: NextAuthPageProps) { }); } -export const getNextAuthPage = (props: NextAuthPageProps) => () => +export const getNextAuthPage = (props: KeystoneOAuth) => () => NextAuthPage({ ...props }); diff --git a/packages/keystone-6-oauth/src/types.ts b/packages/keystone-6-oauth/src/types.ts index a5f818c..709d120 100644 --- a/packages/keystone-6-oauth/src/types.ts +++ b/packages/keystone-6-oauth/src/types.ts @@ -1,83 +1,71 @@ import type { ServerResponse, IncomingMessage } from 'http'; -// TODO: Review this type - why "Cannot find module 'next/server'"? -// @ts-ignore + +import type { + CookiesOptions, + EventCallbacks, + PagesOptions, +} from 'next-auth'; +import type { Provider } from 'next-auth/providers'; +import type { JWTOptions } from 'next-auth/jwt'; + import type { NextRequest } from 'next/server'; -import { Provider } from 'next-auth/providers'; -import { CookiesOptions, PagesOptions } from 'next-auth'; -import { + +import type { BaseListTypeInfo, KeystoneConfig, CreateContext, KeystoneContext, } from '@keystone-6/core/types'; -export type OAuthCallbacks = { - // TODO: Review definition of this type - // eslint-disable-next-line no-unused-vars - onSignIn?: (args: { - account: any; - profile: any; - context: KeystoneContext; - user: any; - }) => Promise; - // TODO: Review definition of this type - // eslint-disable-next-line no-unused-vars - onSignUp?: (args: { - account: any; - created?: any; - profile: any; - context: KeystoneContext; - user: any; - }) => Promise; +type NextAuthResponse = IncomingMessage & NextRequest; + +export type KeystoneOAuthOnSignIn = { + account: any; + profile: any; + context: KeystoneContext; + user: any; +}; +export type KeystoneOAuthOnSignUp = { + account: any; + created?: any; + profile: any; + context: KeystoneContext; + user: any; }; -type NextAuthResponse = IncomingMessage & NextRequest; +export type KeystoneOAuthCallbacks = { + onSignIn?: (args: KeystoneOAuthOnSignIn) => Promise; + onSignUp?: (args: KeystoneOAuthOnSignUp) => Promise; +}; export type NextAuthTemplateProps = { autoCreate: boolean; + context?: KeystoneContext; identityField: string; listKey: string; sessionData: string | undefined; sessionSecret: string; }; -export type OAuthCallbacks = { - // TODO: Review definition of this type - // eslint-disable-next-line no-unused-vars - onSignIn?: (args: { - account: any; - profile: any; - context: KeystoneContext; - user: any; - }) => Promise; - // TODO: Review definition of this type - // eslint-disable-next-line no-unused-vars - onSignUp?: (args: { - account: any; - created?: any; - profile: any; - context: KeystoneContext; - user: any; - }) => Promise; -}; +export type KeystoneOAuth = { + cookies?: Partial; + events?: Partial; + jwt?: Partial; + pages?: Partial; + providers: Provider[]; +} & KeystoneOAuthCallbacks & NextAuthTemplateProps; export declare type AuthSessionStrategy = { - // TODO: Review definition - // eslint-disable-next-line no-unused-vars start: (args: { res: ServerResponse; data: any; createContext: CreateContext; }) => Promise; - // TODO: Review definition - // eslint-disable-next-line no-unused-vars end: (args: { req: IncomingMessage; res: ServerResponse; createContext: CreateContext; }) => Promise; - // TODO: Review definition - // eslint-disable-next-line no-unused-vars get: (args: { req: NextAuthResponse; createContext: CreateContext; @@ -88,15 +76,16 @@ export type NextAuthProviders = Provider[]; type KeystoneOAuthOptions = { /** KeystoneContext */ - context: KeystoneContext; + context?: KeystoneContext; // Custom pages for different NextAuth events pages?: Partial; /** Providers for Next Auth */ providers: NextAuthProviders; }; + type NextAuthOptions = { cookies?: Partial; -} & OAuthCallbacks; +} & KeystoneOAuthCallbacks; export type KeystoneOAuthConfig = KeystoneConfig & KeystoneOAuthOptions & @@ -117,7 +106,7 @@ export type AuthConfig = { sessionData?: string | undefined; /** Next-Auth Session Secret */ sessionSecret: string; -} & OAuthCallbacks & KeystoneOAuthOptions; +} & KeystoneOAuthCallbacks & KeystoneOAuthOptions; export type AuthTokenRequestErrorCode = | 'IDENTITY_NOT_FOUND'