diff --git a/.changeset/small-monkeys-battle.md b/.changeset/small-monkeys-battle.md new file mode 100644 index 0000000000..aeed0197af --- /dev/null +++ b/.changeset/small-monkeys-battle.md @@ -0,0 +1,6 @@ +--- +"@blitzjs/auth": patch +"blitz": patch +--- + +Fix Next-Auth integration: `Unable to use next-auth with provider: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]` diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0dd3bd5180..3f75398f56 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,7 +17,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: pnpm/action-setup@646cdf48217256a3d0b80361c5a50727664284f2 + - uses: pnpm/action-setup@v2.2.4 with: version: 7.33.0 - name: Setup node @@ -38,7 +38,7 @@ jobs: name: Build steps: - uses: actions/checkout@v2 - - uses: pnpm/action-setup@646cdf48217256a3d0b80361c5a50727664284f2 + - uses: pnpm/action-setup@v2.2.4 with: version: 7.33.0 - name: Setup node @@ -68,7 +68,7 @@ jobs: uses: actions/checkout@v3 - name: Setup PNPM - uses: pnpm/action-setup@646cdf48217256a3d0b80361c5a50727664284f2 + uses: pnpm/action-setup@v2.2.4 with: version: 7.33.0 @@ -127,7 +127,7 @@ jobs: uses: actions/checkout@v3 - name: Setup PNPM - uses: pnpm/action-setup@646cdf48217256a3d0b80361c5a50727664284f2 + uses: pnpm/action-setup@v2.2.4 with: version: 7.33.0 diff --git a/apps/toolkit-app/next.config.js b/apps/toolkit-app/next.config.js index 207854be2e..3e7f497505 100644 --- a/apps/toolkit-app/next.config.js +++ b/apps/toolkit-app/next.config.js @@ -1,8 +1,8 @@ -const { withNextAuthAdapter } = require("@blitzjs/auth/next-auth") +const { withNextAuthAdapter } = require("@blitzjs/auth") const { withBlitz } = require("@blitzjs/next") /** - * @type {import('@blitzjs/next').BlitzConfig} + * @type {import('next').NextConfig} **/ const config = { reactStrictMode: true, diff --git a/apps/toolkit-app/src/pages/index.tsx b/apps/toolkit-app/src/pages/index.tsx index e1e3d07538..ad0f28d5b9 100644 --- a/apps/toolkit-app/src/pages/index.tsx +++ b/apps/toolkit-app/src/pages/index.tsx @@ -38,10 +38,10 @@ const UserInfo = () => { } else { return ( <> - + Sign Up - + Login diff --git a/packages/blitz-auth/package.json b/packages/blitz-auth/package.json index 20d858b207..2fda5b4a6d 100644 --- a/packages/blitz-auth/package.json +++ b/packages/blitz-auth/package.json @@ -37,6 +37,7 @@ "cookie": "0.4.1", "cookie-session": "2.0.0", "debug": "4.3.3", + "find-up": "4.1.0", "http": "0.0.1-security", "jsonwebtoken": "9.0.0", "nanoid": "3.2.0", @@ -44,6 +45,7 @@ "openid-client": "5.2.1", "passport": "0.6.0", "path": "0.12.7", + "resolve-from": "5.0.0", "supports-color": "8.1.1", "url": "0.11.0" }, diff --git a/packages/blitz-auth/src/server/adapters/index.ts b/packages/blitz-auth/src/server/adapters/index.ts index 0cefe34c4d..b5a99e222f 100644 --- a/packages/blitz-auth/src/server/adapters/index.ts +++ b/packages/blitz-auth/src/server/adapters/index.ts @@ -1 +1,2 @@ export * from "./passport/adapter" +export * from "./next-auth/webpack" diff --git a/packages/blitz-auth/src/server/adapters/next-auth.ts b/packages/blitz-auth/src/server/adapters/next-auth.ts index 2dcf9b0ec4..175e001778 100644 --- a/packages/blitz-auth/src/server/adapters/next-auth.ts +++ b/packages/blitz-auth/src/server/adapters/next-auth.ts @@ -1,3 +1,2 @@ export * from "./next-auth/adapter" export * from "./next-auth/types" -export * from "./next-auth/webpack" diff --git a/packages/blitz-auth/src/server/adapters/next-auth/adapter.ts b/packages/blitz-auth/src/server/adapters/next-auth/adapter.ts index 8b8abaf20f..738cf3787a 100644 --- a/packages/blitz-auth/src/server/adapters/next-auth/adapter.ts +++ b/packages/blitz-auth/src/server/adapters/next-auth/adapter.ts @@ -27,7 +27,9 @@ import type { } from "./types" import {Provider} from "next-auth/providers" -export {withNextAuthAdapter} from "./webpack" +import {init} from "next-auth/core/init" +import getAuthorizationUrl from "next-auth/core/lib/oauth/authorization-url" +import oAuthCallback from "next-auth/core/lib/oauth/callback" const INTERNAL_REDIRECT_URL_KEY = "_redirectUrl" @@ -102,7 +104,6 @@ export function NextAuthAdapter

( if (providerId?.includes("?")) { providerId = providerId.split("?")[0] } - const {init} = await import("next-auth/core/init").then((m) => m) const {options, cookies} = await init({ // @ts-ignore url: new URL( @@ -158,9 +159,6 @@ async function AuthHandler

( if (action === "login") { middleware.push(async (req, res, next) => { try { - const getAuthorizationUrl = await import("next-auth/core/lib/oauth/authorization-url").then( - (m) => m.default, - ) const _signin = await getAuthorizationUrl({options: options, query: req.query}) if (_signin.cookies) cookies.push(..._signin.cookies) const session = res.blitzCtx.session as SessionContext @@ -191,9 +189,6 @@ async function AuthHandler

( middleware.push( // eslint-disable-next-line no-shadow connectMiddleware(async (req, res, next) => { - const oAuthCallback = await import("next-auth/core/lib/oauth/callback").then( - (m) => m.default, - ) try { const {profile, account, OAuthProfile} = await oAuthCallback({ query: internalRequest.query, diff --git a/packages/blitz-auth/src/server/adapters/next-auth/webpack.ts b/packages/blitz-auth/src/server/adapters/next-auth/webpack.ts index e40403cb5f..9fd3da5156 100644 --- a/packages/blitz-auth/src/server/adapters/next-auth/webpack.ts +++ b/packages/blitz-auth/src/server/adapters/next-auth/webpack.ts @@ -1,15 +1,27 @@ //@ts-nocheck +import fs from "fs-extra" +import path from "path" + export function withNextAuthAdapter(nextConfig) { const config = Object.assign({}, nextConfig) - config.webpack = (config) => { - //add a required resolve alias - config.resolve.alias["next-auth/core/lib/oauth/callback"] = - process.cwd() + "/node_modules/next-auth/core/lib/oauth/callback.js" - config.resolve.alias["next-auth/core/lib/oauth/authorization-url"] = - process.cwd() + "/node_modules/next-auth/core/lib/oauth/authorization-url.js" - config.resolve.alias["next-auth/core/init"] = - process.cwd() + "/node_modules/next-auth/core/init.js" + const nextAuthPath = path.dirname(require.resolve("next-auth")) + const webpack = (config) => { + config.resolve.alias = { + ...config.resolve.alias, + "next-auth/core/lib/oauth/callback": path.join(nextAuthPath, "core/lib/oauth/callback.js"), + "next-auth/core/lib/oauth/authorization-url": path.join( + nextAuthPath, + "core/lib/oauth/authorization-url.js", + ), + "next-auth/core/init": path.join(nextAuthPath, "core/init.js"), + } return config } + if (typeof nextConfig.webpack === "function") { + config.webpack = (config, options) => { + return nextConfig.webpack(webpack(config), options) + } + } + config.webpack = webpack return config } diff --git a/packages/blitz-next/package.json b/packages/blitz-next/package.json index 1a8c782059..4199abd2f5 100644 --- a/packages/blitz-next/package.json +++ b/packages/blitz-next/package.json @@ -40,7 +40,8 @@ "peerDependencies": { "blitz": "2.0.0-beta.28", "next": "*", - "react": "*" + "react": "*", + "tslog": "*" }, "devDependencies": { "@blitzjs/config": "workspace:2.0.0-beta.28", @@ -49,6 +50,7 @@ "@testing-library/react": "13.4.0", "@testing-library/react-hooks": "8.0.1", "@testing-library/user-event": "13.5.0", + "@types/debug": "4.1.7", "@types/node": "18.11.9", "@types/react": "18.0.25", "@types/react-dom": "17.0.14", @@ -62,6 +64,7 @@ "react-dom": "18.2.0", "resolve-from": "5.0.0", "ts-jest": "27.1.4", + "tslog": "4.8.2", "typescript": "^4.8.4", "unbuild": "0.7.6", "watch": "1.0.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5fa2c7f7a8..2a0837399b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -956,7 +956,7 @@ importers: version: 8.27.0 eslint-config-next: specifier: latest - version: 13.4.4(eslint@8.27.0)(typescript@4.8.4) + version: 13.4.5(eslint@8.27.0)(typescript@4.8.4) eslint-plugin-testing-library: specifier: 5.0.1 version: 5.0.1(eslint@8.27.0)(typescript@4.8.4) @@ -1544,6 +1544,9 @@ importers: debug: specifier: 4.3.3 version: 4.3.3(supports-color@8.1.1) + find-up: + specifier: 4.1.0 + version: 4.1.0 http: specifier: 0.0.1-security version: 0.0.1-security @@ -1565,6 +1568,9 @@ importers: path: specifier: 0.12.7 version: 0.12.7 + resolve-from: + specifier: 5.0.0 + version: 5.0.0 supports-color: specifier: 8.1.1 version: 8.1.1 @@ -1666,6 +1672,9 @@ importers: "@testing-library/user-event": specifier: 13.5.0 version: 13.5.0(@testing-library/dom@8.13.0) + "@types/debug": + specifier: 4.1.7 + version: 4.1.7 "@types/node": specifier: 18.11.9 version: 18.11.9 @@ -1705,6 +1714,9 @@ importers: ts-jest: specifier: 27.1.4 version: 27.1.4(typescript@4.8.4) + tslog: + specifier: 4.8.2 + version: 4.8.2 typescript: specifier: ^4.8.4 version: 4.8.4 @@ -2344,7 +2356,7 @@ packages: engines: {node: ">=6.0.0"} dependencies: "@jridgewell/gen-mapping": 0.1.1 - "@jridgewell/trace-mapping": 0.3.11 + "@jridgewell/trace-mapping": 0.3.17 /@babel/code-frame@7.16.7: resolution: @@ -5801,7 +5813,7 @@ packages: engines: {node: ">=6.0.0"} dependencies: "@jridgewell/set-array": 1.1.1 - "@jridgewell/sourcemap-codec": 1.4.13 + "@jridgewell/sourcemap-codec": 1.4.14 /@jridgewell/gen-mapping@0.3.1: resolution: @@ -5811,8 +5823,8 @@ packages: engines: {node: ">=6.0.0"} dependencies: "@jridgewell/set-array": 1.1.1 - "@jridgewell/sourcemap-codec": 1.4.13 - "@jridgewell/trace-mapping": 0.3.11 + "@jridgewell/sourcemap-codec": 1.4.14 + "@jridgewell/trace-mapping": 0.3.17 /@jridgewell/gen-mapping@0.3.2: resolution: @@ -5825,13 +5837,6 @@ packages: "@jridgewell/sourcemap-codec": 1.4.14 "@jridgewell/trace-mapping": 0.3.17 - /@jridgewell/resolve-uri@3.0.7: - resolution: - { - integrity: sha512-8cXDaBBHOr2pQ7j77Y6Vp5VDT2sIqWyWQ56TjEq4ih/a4iST3dItRe8Q9fp0rrIl9DoKhWQtUQz/YpOxLkXbNA==, - } - engines: {node: ">=6.0.0"} - /@jridgewell/resolve-uri@3.1.0: resolution: { @@ -5846,27 +5851,12 @@ packages: } engines: {node: ">=6.0.0"} - /@jridgewell/sourcemap-codec@1.4.13: - resolution: - { - integrity: sha512-GryiOJmNcWbovBxTfZSF71V/mXbgcV3MewDe3kIMCLyIh5e7SKAeUZs+rMnJ8jkMolZ/4/VsdBmMrw3l+VdZ3w==, - } - /@jridgewell/sourcemap-codec@1.4.14: resolution: { integrity: sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw==, } - /@jridgewell/trace-mapping@0.3.11: - resolution: - { - integrity: sha512-RllI476aSMsxzeI9TtlSMoNTgHDxEmnl6GkkHwhr0vdL8W+0WuesyI8Vd3rBOfrwtPXbPxdT9ADJdiOKgzxPQA==, - } - dependencies: - "@jridgewell/resolve-uri": 3.0.7 - "@jridgewell/sourcemap-codec": 1.4.13 - /@jridgewell/trace-mapping@0.3.17: resolution: { @@ -5882,8 +5872,8 @@ packages: integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==, } dependencies: - "@jridgewell/resolve-uri": 3.0.7 - "@jridgewell/sourcemap-codec": 1.4.13 + "@jridgewell/resolve-uri": 3.1.0 + "@jridgewell/sourcemap-codec": 1.4.14 /@juanm04/cpx@2.0.1(supports-color@8.1.1): resolution: @@ -6001,10 +5991,10 @@ packages: glob: 7.1.7 dev: true - /@next/eslint-plugin-next@13.4.4: + /@next/eslint-plugin-next@13.4.5: resolution: { - integrity: sha512-5jnh7q6I15efnjR/rR+/TGTc9hn53g3JTbEjAMjmeQiExKqEUgIXqrHI5zlTNlNyzCPkBB860/ctxXheZaF2Vw==, + integrity: sha512-/xD/kyJhXmBZq+0xGKOdjL22c9/4i3mBAXaU9aOGEHTXqqFeOz8scJbScWF13aMqigeoFCsDqngIB2MIatcn4g==, } dependencies: glob: 7.1.7 @@ -12007,10 +11997,10 @@ packages: - supports-color dev: true - /eslint-config-next@13.4.4(eslint@8.27.0)(typescript@4.8.4): + /eslint-config-next@13.4.5(eslint@8.27.0)(typescript@4.8.4): resolution: { - integrity: sha512-z/PMbm6L0iC/fwISULxe8IVy4DtNqZk2wQY711o35klenq70O6ns82A8yuMVCFjHC0DIyB2lyugesRtuk9u8dQ==, + integrity: sha512-7qgJmRp9ClRzPgkzEz7ahK+Rasiv4k2aU3eqkkORzseNUGdtImZVYomcXUhUheHwkxzdN2p//nbIA7zJrCxsCg==, } peerDependencies: eslint: ^7.23.0 || ^8.0.0 @@ -12019,7 +12009,7 @@ packages: typescript: optional: true dependencies: - "@next/eslint-plugin-next": 13.4.4 + "@next/eslint-plugin-next": 13.4.5 "@rushstack/eslint-patch": 1.1.3 "@typescript-eslint/parser": 5.43.0(eslint@8.27.0)(typescript@4.8.4) eslint: 8.27.0 @@ -15270,7 +15260,7 @@ packages: pretty-format: 29.2.1 slash: 3.0.0 strip-json-comments: 3.1.1 - ts-node: 10.9.1(typescript@4.8.4) + ts-node: 10.9.1(@types/node@18.11.9)(typescript@4.8.4) transitivePeerDependencies: - supports-color @@ -21058,7 +21048,7 @@ packages: "@tsconfig/node14": 1.0.2 "@tsconfig/node16": 1.0.3 "@types/node": 18.11.9 - acorn: 8.7.1 + acorn: 8.8.1 acorn-walk: 8.2.0 arg: 4.1.3 create-require: 1.1.1 @@ -21067,7 +21057,6 @@ packages: typescript: 4.8.4 v8-compile-cache-lib: 3.0.1 yn: 3.1.1 - dev: false /ts-node@10.9.1(@types/node@18.7.13)(typescript@4.8.4): resolution: @@ -21092,7 +21081,7 @@ packages: "@tsconfig/node14": 1.0.2 "@tsconfig/node16": 1.0.3 "@types/node": 18.7.13 - acorn: 8.7.1 + acorn: 8.8.1 acorn-walk: 8.2.0 arg: 4.1.3 create-require: 1.1.1 @@ -21125,7 +21114,7 @@ packages: "@tsconfig/node12": 1.0.10 "@tsconfig/node14": 1.0.2 "@tsconfig/node16": 1.0.3 - acorn: 8.7.1 + acorn: 8.8.1 acorn-walk: 8.2.0 arg: 4.1.3 create-require: 1.1.1 @@ -21134,6 +21123,7 @@ packages: typescript: 4.8.4 v8-compile-cache-lib: 3.0.1 yn: 3.1.1 + dev: false /tsconfig-paths@3.14.1: resolution: @@ -21174,7 +21164,6 @@ packages: integrity: sha512-eAKIRjxfSKYLs06r1wT7oou6Uv9VN6NW9g0JPidBlqQwPBBl5+84dm7r8zSOPVq1kyfEw1P6B3/FLSpZCorAgA==, } engines: {node: ">=16"} - dev: false /tsscmp@1.0.6: resolution: @@ -22199,7 +22188,7 @@ packages: engines: {node: ">= 10.13.0"} hasBin: true dependencies: - acorn: 8.7.1 + acorn: 8.8.1 acorn-walk: 8.2.0 chalk: 4.1.2 commander: 6.2.1