diff --git a/.eslintrc.js b/.eslintrc.js index be03fc3c..1033fc27 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -43,6 +43,7 @@ module.exports = { // Let eslint manage semicolons '@typescript-eslint/no-extra-semi': 0, 'import/no-unresolved': 'error', + 'import/prefer-default-export': 0, 'import/extensions': [ 'error', 'ignorePackages', diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index f6fc65ca..00000000 --- a/index.d.ts +++ /dev/null @@ -1,167 +0,0 @@ -import * as Cookies from 'cookies' -import type { User } from 'firebase/auth' -import type { - GetServerSideProps, - GetServerSidePropsContext, - GetServerSidePropsResult, - NextApiRequest, - NextApiResponse, - PreviewData, -} from 'next' -import type { ComponentType } from 'react' -import type { ParsedUrlQuery } from 'querystring' - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -type Dictionary = Record - -export enum AuthAction { - RENDER = 'render', - SHOW_LOADER = 'showLoader', - RETURN_NULL = 'returnNull', - REDIRECT_TO_LOGIN = 'redirectToLogin', - REDIRECT_TO_APP = 'redirectToApp', -} - -export interface AuthUser { - id: string | null - email: string | null - emailVerified: boolean - phoneNumber: string | null - displayName: string | null - photoURL: string | null - claims: Record - getIdToken: (forceRefresh?: boolean) => Promise - clientInitialized: boolean - firebaseUser: User | null - signOut: () => Promise -} - -export type SSRPropsContext< - Q extends ParsedUrlQuery = ParsedUrlQuery, - D extends PreviewData = PreviewData -> = GetServerSidePropsContext & { AuthUser: AuthUser } - -export type SSRPropGetter< - P extends Dictionary = Dictionary, - Q extends ParsedUrlQuery = ParsedUrlQuery, - D extends PreviewData = PreviewData -> = (context: SSRPropsContext) => Promise> - -interface AuthUserContext extends AuthUser { - serialize: (opts?: { includeToken?: boolean }) => string -} - -type URLResolveFunction = (obj: { - ctx: GetServerSidePropsContext - AuthUser: AuthUser -}) => string | RedirectObject - -type RedirectObject = { - destination: string | URLResolveFunction - basePath: boolean -} - -type PageURL = string | RedirectObject | URLResolveFunction - -interface InitConfig { - authPageURL?: PageURL - appPageURL?: PageURL - loginAPIEndpoint?: string - logoutAPIEndpoint?: string - onVerifyTokenError?: (error: unknown) => void - onTokenRefreshError?: (error: unknown) => void - tokenChangedHandler?: (user: AuthUser) => void - onLoginRequestError?: (error: unknown) => void - onLogoutRequestError?: (error: unknown) => void - useFirebaseAdminDefaultCredential?: boolean - firebaseAdminInitConfig?: { - credential: { - projectId: string - clientEmail: string - privateKey: string - } - databaseURL?: string - } - firebaseAuthEmulatorHost?: string - firebaseClientInitConfig: { - apiKey: string - projectId?: string - appId?: string - // "PROJECT_ID.firebaseapp.com" - authDomain?: string - // "https://PROJECT_ID.firebaseio.com" - databaseURL?: string - // "PROJECT_ID.appspot.com" - storageBucket?: string - // "SENDER_ID" - messagingSenderId?: string - // "G-MEASUREMENT_ID" - measurementId?: string - } - cookies: Cookies.Option & - Cookies.SetOption & { - name: string - } - debug?: boolean -} - -export const init: (config: InitConfig) => void - -export const setAuthCookies: ( - req: NextApiRequest, - res: NextApiResponse -) => Promise<{ - idToken: string - refreshToken: string - AuthUser: AuthUser -}> - -export const unsetAuthCookies: ( - req: NextApiRequest, - res: NextApiResponse -) => Promise - -export const getUserFromCookies: (options: { - req?: NextApiRequest - includeToken?: boolean - authCookieValue?: string - authCookieSigValue?: string -}) => Promise - -export const useAuthUser: () => AuthUserContext - -export const verifyIdToken: (token: string) => Promise - -export const withAuthUser:

(options?: { - whenAuthed?: AuthAction.RENDER | AuthAction.REDIRECT_TO_APP - whenAuthedBeforeRedirect?: - | AuthAction.RENDER - | AuthAction.SHOW_LOADER - | AuthAction.RETURN_NULL - whenUnauthedBeforeInit?: - | AuthAction.RENDER - | AuthAction.REDIRECT_TO_LOGIN - | AuthAction.SHOW_LOADER - | AuthAction.RETURN_NULL - whenUnauthedAfterInit?: AuthAction.RENDER | AuthAction.REDIRECT_TO_LOGIN - appPageURL?: PageURL - authPageURL?: PageURL - LoaderComponent?: ComponentType | null -}) => (component: ComponentType

) => ComponentType

- -type GetServerSidePropsAuthWrapper = (options?: { - whenAuthed?: AuthAction.RENDER | AuthAction.REDIRECT_TO_APP - whenUnauthed?: AuthAction.RENDER | AuthAction.REDIRECT_TO_LOGIN - appPageURL?: PageURL - authPageURL?: PageURL -}) => < - P extends Dictionary = Dictionary, - Q extends ParsedUrlQuery = ParsedUrlQuery, - D extends PreviewData = PreviewData ->( - propGetter?: SSRPropGetter -) => GetServerSideProps - -export const withAuthUserTokenSSR: GetServerSidePropsAuthWrapper - -export const withAuthUserSSR: GetServerSidePropsAuthWrapper diff --git a/package.json b/package.json index 7b829b03..20e4d4ef 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,10 @@ "author": "Gladly Team", "license": "MIT", "scripts": { - "build": "npm-run-all -s build:clean build:src", + "build": "npm-run-all -s build:clean build:src build:types-declaration", "build:clean": "rm -rf ./build", "build:src": "NODE_ENV=production webpack", + "build:types-declaration": "dts-bundle-generator ./src/index.server.ts --out-file ./build/index.d.ts", "lint": "tsc --noEmit && eslint ./", "bundlesize": "npm-run-all -s build bundlesize:no-build", "bundlesize:no-build": "bundlesize", @@ -58,6 +59,7 @@ "copy-webpack-plugin": "^11.0.0", "core-js": "^3.23.5", "datwd": "^0.2.0", + "dts-bundle-generator": "^8.0.1", "eslint": "^8.20.0", "eslint-config-airbnb": "^19.0.4", "eslint-config-prettier": "^8.5.0", diff --git a/src/AuthAction.ts b/src/AuthAction.ts index ff23dfe2..49500b90 100644 --- a/src/AuthAction.ts +++ b/src/AuthAction.ts @@ -1,12 +1,10 @@ // Different behaviors when the user's auth status is pending // or mismatches the page requirements. -enum AuthAction { +export enum AuthAction { RENDER = 'render', SHOW_LOADER = 'showLoader', RETURN_NULL = 'returnNull', REDIRECT_TO_LOGIN = 'redirectToLogin', REDIRECT_TO_APP = 'redirectToApp', } - -export default AuthAction diff --git a/src/__tests__/AuthAction.test.ts b/src/__tests__/AuthAction.test.ts deleted file mode 100644 index 6bce8292..00000000 --- a/src/__tests__/AuthAction.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -// eslint-disable-next-line jest/no-export -export {} - -describe('index.ts: AuthAction', () => { - it('defines the expected constants', () => { - expect.assertions(1) - const AuthAction = require('src/AuthAction').default - expect(AuthAction).toEqual({ - RENDER: 'render', - SHOW_LOADER: 'showLoader', - RETURN_NULL: 'returnNull', - REDIRECT_TO_LOGIN: 'redirectToLogin', - REDIRECT_TO_APP: 'redirectToApp', - }) - }) -}) diff --git a/src/__tests__/index.server.moduleLoad.test.ts b/src/__tests__/index.server.moduleLoad.test.ts index f5ef709e..0717ef5d 100644 --- a/src/__tests__/index.server.moduleLoad.test.ts +++ b/src/__tests__/index.server.moduleLoad.test.ts @@ -1,4 +1,4 @@ -// Tests for index.server.js that require resetting all +// Tests for index.server.ts that require resetting all // modules between tests. Most tests should reside in // index.server.test.js. // eslint-disable-next-line jest/no-export @@ -10,7 +10,7 @@ afterEach(() => { jest.resetModules() }) -describe('index.server.js (resetting modules)', () => { +describe('index.server.ts (resetting modules)', () => { it('imports without error when missing optional dependencies', () => { expect.assertions(0) @@ -44,7 +44,7 @@ describe('index.server.js (resetting modules)', () => { }) // eslint-disable-next-line no-unused-expressions - require('src/index.server').default + require('src/index.server') }) it('throws an error when calling useAuthUser without react installed', () => { @@ -59,7 +59,7 @@ describe('index.server.js (resetting modules)', () => { moduleName: 'react', }) }) - const { useAuthUser } = require('src/index.server').default + const { useAuthUser } = require('src/index.server') expect(() => { useAuthUser() }).toThrow(expectedErr) diff --git a/src/__tests__/index.server.test.ts b/src/__tests__/index.server.test.ts index fc011d94..6308b701 100644 --- a/src/__tests__/index.server.test.ts +++ b/src/__tests__/index.server.test.ts @@ -24,38 +24,38 @@ afterEach(() => { jest.clearAllMocks() }) -describe('index.server.js: init', () => { +describe('index.server.ts: init', () => { it('exports init', () => { expect.assertions(2) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.init).toBeDefined() expect(indexServer.init).toEqual(expect.any(Function)) }) it('calls setDebugEnabled with true if config.debug is true', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') indexServer.init({ debug: true }) expect(setDebugEnabled).toHaveBeenCalledWith(true) }) it('calls setDebugEnabled with false if config.debug is truthy but non-true', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') indexServer.init({ debug: 'yes' }) expect(setDebugEnabled).toHaveBeenCalledWith(false) }) it('calls setDebugEnabled with false if config.debug is false', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') indexServer.init({ debug: false }) expect(setDebugEnabled).toHaveBeenCalledWith(false) }) it('calls setConfig with the provided config', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') indexServer.init({ some: 'config' }) expect(setConfig).toHaveBeenCalledWith({ some: 'config' }) }) @@ -64,7 +64,7 @@ describe('index.server.js: init', () => { // https://github.com/gladly-team/next-firebase-auth/issues/70 it('does not call initFirebaseAdminSDK', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') indexServer.init({ fake: 'config' }) expect(initFirebaseAdminSDK).not.toHaveBeenCalled() }) @@ -73,14 +73,14 @@ describe('index.server.js: init', () => { describe('index.server.js: withAuthUser', () => { it('exports withAuthUser', () => { expect.assertions(2) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.withAuthUser).toBeDefined() expect(indexServer.withAuthUser).toEqual(expect.any(Function)) }) it('calls the withAuthUser module', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') indexServer.withAuthUser({ appPageURL: '/my-fake-app-page' }) expect(withAuthUser).toHaveBeenCalledWith({ appPageURL: '/my-fake-app-page', @@ -91,14 +91,14 @@ describe('index.server.js: withAuthUser', () => { describe('index.server.js: useAuthUser', () => { it('exports useAuthUser', () => { expect.assertions(2) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.useAuthUser).toBeDefined() expect(indexServer.useAuthUser).toEqual(expect.any(Function)) }) it('calls the useAuthUser module', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') indexServer.useAuthUser() expect(useAuthUser).toHaveBeenCalled() }) @@ -107,14 +107,14 @@ describe('index.server.js: useAuthUser', () => { describe('index.server.js: withAuthUserSSR', () => { it('exports withAuthUserSSR', () => { expect.assertions(2) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.withAuthUserSSR).toBeDefined() expect(indexServer.withAuthUserSSR).toEqual(expect.any(Function)) }) it('calls the withAuthUserTokenSSR module with useToken=false', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') indexServer.withAuthUserSSR({ some: 'options' }) expect(withAuthUserTokenSSR).toHaveBeenCalledWith( { some: 'options' }, @@ -126,14 +126,14 @@ describe('index.server.js: withAuthUserSSR', () => { describe('index.server.js: withAuthUserTokenSSR', () => { it('exports withAuthUserTokenSSR', () => { expect.assertions(2) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.withAuthUserTokenSSR).toBeDefined() expect(indexServer.withAuthUserTokenSSR).toEqual(expect.any(Function)) }) it('calls the withAuthUserTokenSSR module with useToken=true', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') indexServer.withAuthUserTokenSSR({ some: 'options' }) expect(withAuthUserTokenSSR).toHaveBeenCalledWith( { some: 'options' }, @@ -145,14 +145,14 @@ describe('index.server.js: withAuthUserTokenSSR', () => { describe('index.server.js: setAuthCookies', () => { it('exports setAuthCookies', () => { expect.assertions(2) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.setAuthCookies).toBeDefined() expect(indexServer.setAuthCookies).toEqual(expect.any(Function)) }) it('exports the expected module', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.setAuthCookies).toEqual(setAuthCookies) }) }) @@ -160,14 +160,14 @@ describe('index.server.js: setAuthCookies', () => { describe('index.server.js: unsetAuthCookies', () => { it('exports unsetAuthCookies', () => { expect.assertions(2) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.unsetAuthCookies).toBeDefined() expect(indexServer.unsetAuthCookies).toEqual(expect.any(Function)) }) it('exports the expected module', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.unsetAuthCookies).toEqual(unsetAuthCookies) }) }) @@ -175,14 +175,14 @@ describe('index.server.js: unsetAuthCookies', () => { describe('index.server.js: verifyIdToken', () => { it('exports verifyIdToken', () => { expect.assertions(2) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.verifyIdToken).toBeDefined() expect(indexServer.verifyIdToken).toEqual(expect.any(Function)) }) it('exports the expected module', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.verifyIdToken).toEqual(verifyIdToken) }) }) @@ -190,14 +190,14 @@ describe('index.server.js: verifyIdToken', () => { describe('index.server.js: getUserFromCookies', () => { it('exports getUserFromCookies', () => { expect.assertions(2) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.getUserFromCookies).toBeDefined() expect(indexServer.getUserFromCookies).toEqual(expect.any(Function)) }) it('exports the expected module', () => { expect.assertions(1) - const indexServer = require('src/index.server').default + const indexServer = require('src/index.server') expect(indexServer.getUserFromCookies).toEqual(getUserFromCookies) }) }) diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 51d662b6..264e1f40 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -21,35 +21,35 @@ afterEach(() => { describe('index.ts: init', () => { it('exports init', () => { expect.assertions(2) - const index = require('src/index').default + const index = require('src/index') expect(index.init).toBeDefined() expect(index.init).toEqual(expect.any(Function)) }) it('calls setDebugEnabled with true if config.debug is true', () => { expect.assertions(1) - const index = require('src/index').default + const index = require('src/index') index.init({ debug: true }) expect(setDebugEnabled).toHaveBeenCalledWith(true) }) it('calls setDebugEnabled with false if config.debug is truthy but non-true', () => { expect.assertions(1) - const index = require('src/index').default + const index = require('src/index') index.init({ debug: 'yes' }) expect(setDebugEnabled).toHaveBeenCalledWith(false) }) it('calls setDebugEnabled with false if config.debug is false', () => { expect.assertions(1) - const index = require('src/index').default + const index = require('src/index') index.init({ debug: false }) expect(setDebugEnabled).toHaveBeenCalledWith(false) }) it('calls setConfig with the provided config', () => { expect.assertions(1) - const index = require('src/index').default + const index = require('src/index') index.init({ some: 'config' }) expect(setConfig).toHaveBeenCalledWith({ some: 'config' }) }) @@ -57,7 +57,7 @@ describe('index.ts: init', () => { it('calls initFirebaseClientSDK if on the client side', () => { expect.assertions(1) mockIsClientSide.mockReturnValue(true) - const index = require('src/index').default + const index = require('src/index') index.init() expect(initFirebaseClientSDK).toHaveBeenCalled() }) @@ -65,7 +65,7 @@ describe('index.ts: init', () => { it('does not call initFirebaseClientSDK if on the server side', () => { expect.assertions(1) mockIsClientSide.mockReturnValue(false) - const index = require('src/index').default + const index = require('src/index') index.init() expect(initFirebaseClientSDK).not.toHaveBeenCalled() }) @@ -74,7 +74,7 @@ describe('index.ts: init', () => { describe('index.ts: withAuthUser', () => { it('exports withAuthUser', () => { expect.assertions(2) - const index = require('src/index').default + const index = require('src/index') expect(index.withAuthUser).toBeDefined() expect(index.withAuthUser).toEqual(expect.any(Function)) }) @@ -83,7 +83,7 @@ describe('index.ts: withAuthUser', () => { describe('index.ts: useAuthUser', () => { it('exports useAuthUser', () => { expect.assertions(2) - const index = require('src/index').default + const index = require('src/index') expect(index.useAuthUser).toBeDefined() expect(index.useAuthUser).toEqual(expect.any(Function)) }) @@ -92,7 +92,7 @@ describe('index.ts: useAuthUser', () => { describe('index.ts: withAuthUserSSR', () => { it('exports withAuthUserSSR', () => { expect.assertions(2) - const index = require('src/index').default + const index = require('src/index') expect(index.withAuthUserSSR).toBeDefined() expect(index.withAuthUserSSR).toEqual(expect.any(Function)) }) @@ -100,7 +100,7 @@ describe('index.ts: withAuthUserSSR', () => { it('throws if called on the client side', () => { expect.assertions(1) mockIsClientSide.mockReturnValue(true) - const index = require('src/index').default + const index = require('src/index') expect(() => { index.withAuthUserSSR() }).toThrow('"withAuthUserSSR" can only be called server-side.') @@ -110,7 +110,7 @@ describe('index.ts: withAuthUserSSR', () => { describe('index.ts: withAuthUserTokenSSR', () => { it('exports withAuthUserTokenSSR', () => { expect.assertions(2) - const index = require('src/index').default + const index = require('src/index') expect(index.withAuthUserTokenSSR).toBeDefined() expect(index.withAuthUserTokenSSR).toEqual(expect.any(Function)) }) @@ -118,7 +118,7 @@ describe('index.ts: withAuthUserTokenSSR', () => { it('throws if called on the client side', () => { expect.assertions(1) mockIsClientSide.mockReturnValue(true) - const index = require('src/index').default + const index = require('src/index') expect(() => { index.withAuthUserTokenSSR() }).toThrow('"withAuthUserTokenSSR" can only be called server-side.') @@ -128,7 +128,7 @@ describe('index.ts: withAuthUserTokenSSR', () => { describe('index.ts: setAuthCookies', () => { it('exports setAuthCookies', () => { expect.assertions(2) - const index = require('src/index').default + const index = require('src/index') expect(index.setAuthCookies).toBeDefined() expect(index.setAuthCookies).toEqual(expect.any(Function)) }) @@ -136,7 +136,7 @@ describe('index.ts: setAuthCookies', () => { it('throws if called on the client side', () => { expect.assertions(1) mockIsClientSide.mockReturnValue(true) - const index = require('src/index').default + const index = require('src/index') expect(() => { index.setAuthCookies() }).toThrow('"setAuthCookies" can only be called server-side.') @@ -146,7 +146,7 @@ describe('index.ts: setAuthCookies', () => { describe('index.ts: unsetAuthCookies', () => { it('exports unsetAuthCookies', () => { expect.assertions(2) - const index = require('src/index').default + const index = require('src/index') expect(index.unsetAuthCookies).toBeDefined() expect(index.unsetAuthCookies).toEqual(expect.any(Function)) }) @@ -154,7 +154,7 @@ describe('index.ts: unsetAuthCookies', () => { it('throws if called on the client side', () => { expect.assertions(1) mockIsClientSide.mockReturnValue(true) - const index = require('src/index').default + const index = require('src/index') expect(() => { index.unsetAuthCookies() }).toThrow('"unsetAuthCookies" can only be called server-side.') @@ -164,7 +164,7 @@ describe('index.ts: unsetAuthCookies', () => { describe('index.ts: verifyIdToken', () => { it('exports verifyIdToken', () => { expect.assertions(2) - const index = require('src/index').default + const index = require('src/index') expect(index.verifyIdToken).toBeDefined() expect(index.verifyIdToken).toEqual(expect.any(Function)) }) @@ -172,7 +172,7 @@ describe('index.ts: verifyIdToken', () => { it('throws if called on the client side', () => { expect.assertions(1) mockIsClientSide.mockReturnValue(true) - const index = require('src/index').default + const index = require('src/index') expect(() => { index.verifyIdToken() }).toThrow('"verifyIdToken" can only be called server-side.') @@ -182,7 +182,7 @@ describe('index.ts: verifyIdToken', () => { describe('index.ts: AuthAction', () => { it('defines the expected constants', () => { expect.assertions(1) - const index = require('src/index').default + const index = require('src/index') expect(index.AuthAction).toEqual({ RENDER: 'render', SHOW_LOADER: 'showLoader', @@ -196,7 +196,7 @@ describe('index.ts: AuthAction', () => { describe('index.ts: getUserFromCookies', () => { it('exports getUserFromCookies', () => { expect.assertions(2) - const index = require('src/index').default + const index = require('src/index') expect(index.getUserFromCookies).toBeDefined() expect(index.getUserFromCookies).toEqual(expect.any(Function)) }) @@ -204,7 +204,7 @@ describe('index.ts: getUserFromCookies', () => { it('throws if called on the client side', () => { expect.assertions(1) mockIsClientSide.mockReturnValue(true) - const index = require('src/index').default + const index = require('src/index') expect(() => { index.getUserFromCookies() }).toThrow('"getUserFromCookies" can only be called server-side.') diff --git a/src/__tests__/withAuthUser.test.tsx b/src/__tests__/withAuthUser.test.tsx index 1cc7e760..66fd6f3f 100644 --- a/src/__tests__/withAuthUser.test.tsx +++ b/src/__tests__/withAuthUser.test.tsx @@ -10,7 +10,7 @@ import { import useAuthUser from 'src/useAuthUser' import createAuthUser, { AuthUser as AuthUserType } from 'src/createAuthUser' import useFirebaseUser from 'src/useFirebaseUser' -import AuthAction from 'src/AuthAction' +import { AuthAction } from 'src/AuthAction' import logDebug from 'src/logDebug' import withAuthUser from 'src/withAuthUser' diff --git a/src/__tests__/withAuthUserTokenSSR.test.ts b/src/__tests__/withAuthUserTokenSSR.test.ts index 74bfc5e4..15ee0169 100644 --- a/src/__tests__/withAuthUserTokenSSR.test.ts +++ b/src/__tests__/withAuthUserTokenSSR.test.ts @@ -4,7 +4,7 @@ import getMockConfig from 'src/testHelpers/createMockConfig' import { createMockFirebaseUserAdminSDK } from 'src/testHelpers/authUserInputs' import createAuthUser from 'src/createAuthUser' import createMockNextContext from 'src/testHelpers/createMockNextContext' -import AuthAction from 'src/AuthAction' +import { AuthAction } from 'src/AuthAction' import getUserFromCookies from 'src/getUserFromCookies' import logDebug from 'src/logDebug' import { ConfigInput } from 'src/configTypes' diff --git a/src/index.server.ts b/src/index.server.ts index 2661e726..4cd83b79 100644 --- a/src/index.server.ts +++ b/src/index.server.ts @@ -1,33 +1,33 @@ /* eslint-disable global-require, @typescript-eslint/no-var-requires */ import initCommon from 'src/initCommon' -import AuthAction from 'src/AuthAction' // These are exclusively for server-side use. -import setAuthCookies from 'src/setAuthCookies' -import unsetAuthCookies from 'src/unsetAuthCookies' -import { verifyIdToken } from 'src/firebaseAdmin' -import getUserFromCookies from 'src/getUserFromCookies' +import setAuthCookiesExport from 'src/setAuthCookies' +import unsetAuthCookiesExport from 'src/unsetAuthCookies' +import { verifyIdToken as verifyIdTokenExport } from 'src/firebaseAdmin' +import getUserFromCookiesExport from 'src/getUserFromCookies' import { ConfigInput } from './configTypes' import { WithAuthUserOptions } from './withAuthUser' import { WithAuthUserSSROptions } from './withAuthUserTokenSSR' -const init = (config: ConfigInput) => { +// enum: AuthAction +export * from 'src/AuthAction' + +export const init = (config: ConfigInput) => { initCommon(config) // We only initialize the Firebase admin SDK as it's needed. See: // https://github.com/gladly-team/next-firebase-auth/issues/70 } -const withAuthUser = (options: WithAuthUserOptions) => { - // Require rather than import the module to support optional - // peer dependencies. See: - // https://github.com/gladly-team/next-firebase-auth/issues/502 - const withAuthUserModule = require('src/withAuthUser').default - return withAuthUserModule(options) -} +export const getUserFromCookies = getUserFromCookiesExport + +export const setAuthCookies = setAuthCookiesExport -const useAuthUser = () => { +export const unsetAuthCookies = unsetAuthCookiesExport + +export const useAuthUser = () => { // Some dependencies are optional. Throw if they aren't installed // when calling this API. // https://github.com/gladly-team/next-firebase-auth/issues/502 @@ -43,25 +43,22 @@ const useAuthUser = () => { return useAuthUserModule() } -const withAuthUserSSR = (options: WithAuthUserSSROptions) => { +export const verifyIdToken = verifyIdTokenExport + +export const withAuthUser = (options: WithAuthUserOptions) => { + // Require rather than import the module to support optional + // peer dependencies. See: + // https://github.com/gladly-team/next-firebase-auth/issues/502 + const withAuthUserModule = require('src/withAuthUser').default + return withAuthUserModule(options) +} + +export const withAuthUserSSR = (options: WithAuthUserSSROptions) => { const withAuthUserTokenSSRModule = require('src/withAuthUserTokenSSR').default return withAuthUserTokenSSRModule(options, { useToken: false }) } -const withAuthUserTokenSSR = (options: WithAuthUserSSROptions) => { +export const withAuthUserTokenSSR = (options: WithAuthUserSSROptions) => { const withAuthUserTokenSSRModule = require('src/withAuthUserTokenSSR').default return withAuthUserTokenSSRModule(options, { useToken: true }) } - -export default { - init, - withAuthUser, - useAuthUser, - withAuthUserSSR, - withAuthUserTokenSSR, - setAuthCookies, - unsetAuthCookies, - verifyIdToken, - AuthAction, - getUserFromCookies, -} diff --git a/src/index.ts b/src/index.ts index 3adb8588..9c688e9e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,11 +3,13 @@ import withAuthUserModule from 'src/withAuthUser' import useAuthUserModule from 'src/useAuthUser' import initFirebaseClientSDK from 'src/initFirebaseClientSDK' import isClientSide from 'src/isClientSide' -import AuthAction from 'src/AuthAction' import initCommon from 'src/initCommon' import { ConfigInput } from './configTypes' -const init = (config: ConfigInput) => { +// enum: AuthAction +export * from 'src/AuthAction' + +export const init = (config: ConfigInput) => { initCommon(config) // On the client side, initialize the Firebase JS SDK. @@ -16,43 +18,30 @@ const init = (config: ConfigInput) => { } } -const withAuthUser = withAuthUserModule - -const useAuthUser = useAuthUserModule - -const withAuthUserSSR = () => { - throw new Error('"withAuthUserSSR" can only be called server-side.') -} - -const withAuthUserTokenSSR = () => { - throw new Error('"withAuthUserTokenSSR" can only be called server-side.') +export const getUserFromCookies = () => { + throw new Error('"getUserFromCookies" can only be called server-side.') } -const setAuthCookies = () => { +export const setAuthCookies = () => { throw new Error('"setAuthCookies" can only be called server-side.') } -const unsetAuthCookies = () => { +export const unsetAuthCookies = () => { throw new Error('"unsetAuthCookies" can only be called server-side.') } -const verifyIdToken = () => { +export const useAuthUser = useAuthUserModule + +export const verifyIdToken = () => { throw new Error('"verifyIdToken" can only be called server-side.') } -const getUserFromCookies = () => { - throw new Error('"getUserFromCookies" can only be called server-side.') +export const withAuthUser = withAuthUserModule + +export const withAuthUserSSR = () => { + throw new Error('"withAuthUserSSR" can only be called server-side.') } -export default { - init, - withAuthUser, - useAuthUser, - withAuthUserSSR, - withAuthUserTokenSSR, - setAuthCookies, - unsetAuthCookies, - verifyIdToken, - AuthAction, - getUserFromCookies, +export const withAuthUserTokenSSR = () => { + throw new Error('"withAuthUserTokenSSR" can only be called server-side.') } diff --git a/src/withAuthUser.tsx b/src/withAuthUser.tsx index 65cdc493..8127c53c 100644 --- a/src/withAuthUser.tsx +++ b/src/withAuthUser.tsx @@ -8,7 +8,7 @@ import createAuthUser, { AuthUserSerialized as AuthUserSerializedType, } from 'src/createAuthUser' import useFirebaseUser from 'src/useFirebaseUser' -import AuthAction from 'src/AuthAction' +import { AuthAction } from 'src/AuthAction' import isClientSide from 'src/isClientSide' import logDebug from 'src/logDebug' import { getAppRedirectInfo, getLoginRedirectInfo } from 'src/redirects' diff --git a/src/withAuthUserTokenSSR.ts b/src/withAuthUserTokenSSR.ts index 3e45b663..c3d79aa5 100644 --- a/src/withAuthUserTokenSSR.ts +++ b/src/withAuthUserTokenSSR.ts @@ -6,7 +6,7 @@ import { } from 'next' import getUserFromCookies from 'src/getUserFromCookies' -import AuthAction from 'src/AuthAction' +import { AuthAction } from 'src/AuthAction' import { getLoginRedirectInfo, getAppRedirectInfo } from 'src/redirects' import logDebug from 'src/logDebug' import { AuthUser as AuthUserType } from './createAuthUser' diff --git a/webpack.config.js b/webpack.config.js index e00f4a1d..324eb1d7 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -14,7 +14,6 @@ const sharedConfig = { // filename set in individual configs below. path: path.resolve(__dirname, 'build'), libraryTarget: 'commonjs2', - libraryExport: 'default', }, resolve: { plugins: [new TsconfigPathsPlugin({})], @@ -50,10 +49,6 @@ const sharedConfig = { plugins: [ new CopyPlugin({ patterns: [ - { - from: './index.d.ts', - to: './index.d.ts', - }, { from: './codemod', to: './codemod', diff --git a/yarn.lock b/yarn.lock index 45e64f40..f1e96ea2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3813,6 +3813,14 @@ domexception@^4.0.0: dependencies: webidl-conversions "^7.0.0" +dts-bundle-generator@^8.0.1: + version "8.0.1" + resolved "https://registry.yarnpkg.com/dts-bundle-generator/-/dts-bundle-generator-8.0.1.tgz#faa34f8325d65a960696df20fe7ef0b46ab2dc4e" + integrity sha512-9JVw78/OXdKfq+RUrmpLm6WAUJp+aOUGEHimVqIlOEH2VugRt1I8CVIoQZlirWZko+/SVZkNgpWCyZubUuzzPA== + dependencies: + typescript ">=5.0.2" + yargs "^17.6.0" + duplexer@^0.1.1, duplexer@^0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6" @@ -8011,6 +8019,11 @@ typed-array-length@^1.0.4: for-each "^0.3.3" is-typed-array "^1.1.9" +typescript@>=5.0.2: + version "5.1.6" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.6.tgz#02f8ac202b6dad2c0dd5e0913745b47a37998274" + integrity sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA== + typescript@^4.7.4: version "4.9.5" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.5.tgz#095979f9bcc0d09da324d58d03ce8f8374cbe65a" @@ -8448,7 +8461,7 @@ yargs@^16.2.0: y18n "^5.0.5" yargs-parser "^20.2.2" -yargs@^17.3.1, yargs@^17.7.2: +yargs@^17.3.1, yargs@^17.6.0, yargs@^17.7.2: version "17.7.2" resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.7.2.tgz#991df39aca675a192b816e1e0363f9d75d2aa269" integrity sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==