Skip to content

Commit

Permalink
(fix) Next-Auth integration (#4153)
Browse files Browse the repository at this point in the history
* fix next-auth integration

* Create small-monkeys-battle.md

* Update .changeset/small-monkeys-battle.md

* add a postinstall script for good measure

* add a postinstall script for good measure

* fix lock file

* upgrade pnpm

* fix version

* fix build error

* suggessions

* fix pnpm lock

* fix pnpm lock

* Update main.yml

* Update apps/toolkit-app/next.config.js

* next-auth working again

* Update apps/toolkit-app/.env

* Update packages/blitz-auth/src/server/auth-sessions.ts

* Update packages/blitz-auth/src/server/auth-sessions.ts
  • Loading branch information
siddhsuresh authored Jun 14, 2023
1 parent 61888d1 commit b6b9a1c
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 67 deletions.
6 changes: 6 additions & 0 deletions .changeset/small-monkeys-battle.md
Original file line number Diff line number Diff line change
@@ -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]`
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions apps/toolkit-app/next.config.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions apps/toolkit-app/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const UserInfo = () => {
} else {
return (
<>
<Link href={Routes.SignupPage()} className={styles.button}>
<Link href={"/signup"} className={styles.button}>
<strong>Sign Up</strong>
</Link>
<Link href={Routes.LoginPage()} className={styles.loginButton}>
<Link href={"/login"} className={styles.loginButton}>
<strong>Login</strong>
</Link>
<Link href="/api/auth/github/login" passHref legacyBehavior>
Expand Down
2 changes: 2 additions & 0 deletions packages/blitz-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
"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",
"oauth": "0.10.0",
"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"
},
Expand Down
1 change: 1 addition & 0 deletions packages/blitz-auth/src/server/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./passport/adapter"
export * from "./next-auth/webpack"
1 change: 0 additions & 1 deletion packages/blitz-auth/src/server/adapters/next-auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./next-auth/adapter"
export * from "./next-auth/types"
export * from "./next-auth/webpack"
11 changes: 3 additions & 8 deletions packages/blitz-auth/src/server/adapters/next-auth/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -102,7 +104,6 @@ export function NextAuthAdapter<P extends Provider[]>(
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(
Expand Down Expand Up @@ -158,9 +159,6 @@ async function AuthHandler<P extends Provider[]>(
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
Expand Down Expand Up @@ -191,9 +189,6 @@ async function AuthHandler<P extends Provider[]>(
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,
Expand Down
28 changes: 20 additions & 8 deletions packages/blitz-auth/src/server/adapters/next-auth/webpack.ts
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 4 additions & 1 deletion packages/blitz-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"peerDependencies": {
"blitz": "2.0.0-beta.28",
"next": "*",
"react": "*"
"react": "*",
"tslog": "*"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-beta.28",
Expand All @@ -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",
Expand All @@ -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"
Expand Down
Loading

0 comments on commit b6b9a1c

Please sign in to comment.