Skip to content

Commit

Permalink
feature: Next Auth Adapter (#4046)
Browse files Browse the repository at this point in the history
* initial working

* add user side callback control

* connect to blitz-auth

* better error handling

* initial working after submodule

* Create fast-adults-guess.md

* add submodule init and update to workflow

* remove internal folder

* cleanup

* ignore tests from next-auth

* add redirectUrl config and maintin consistency

* move required files back to internals folder

* remove submodule cloning

* Patch NextAuth

another time

fix import issue

fix import

add path imports for the adapters

Revert "add to blitz-auth"

This reverts commit 246e62f.

add to blitz-auth

pnpm lock fix

cleanup

fix codegen

remove nextauth submodule and internal folder and patch nextauth

* Revert "Patch NextAuth"

This reverts commit 31888fc.

* switch

* DRY

* optional peerDependency

* Delete .gitmodules

* make it work with webpack alias

* fix

* split to new path

* undo unnecessary changes

* more fixes

* add strong typing to nextauth adapter (#4104)

* add strong typing to nextauth adapter

* cleanup

---------

Co-authored-by: Brandon Bayer <b@bayer.ws>
  • Loading branch information
siddhsuresh and flybayer committed Mar 24, 2023
1 parent 3480d90 commit 6f18cbd
Show file tree
Hide file tree
Showing 37 changed files with 1,235 additions and 209 deletions.
6 changes: 6 additions & 0 deletions .changeset/fast-adults-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@blitzjs/auth": minor
"blitz": minor
---

feature: Next Auth Adapter
8 changes: 2 additions & 6 deletions apps/toolkit-app/next.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// @ts-check

const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
})
const { withNextAuthAdapter } = require("@blitzjs/auth/next-auth")
const { withBlitz } = require("@blitzjs/next")

/**
Expand All @@ -12,4 +8,4 @@ const config = {
reactStrictMode: true,
}

module.exports = withBlitz(withBundleAnalyzer(config))
module.exports = withBlitz(withNextAuthAdapter(config))
1 change: 1 addition & 0 deletions apps/toolkit-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@prisma/client": "4.6.1",
"blitz": "workspace:2.0.0-beta.23",
"next": "12.2.5",
"next-auth": "4.18.7",
"prisma": "4.6.1",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
1 change: 0 additions & 1 deletion apps/toolkit-app/src/custom-plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const BlitzCustomPlugin = createClientPlugin<CustomPluginOptions, {}>(
middleware: {
beforeHttpRequest: (req) => {
//make changes to the request options before RPC call
req.headers = { ...req.headers, ...{ customHeader: "customHeaderValue" } }
return req
},
beforeHttpResponse: (res) => {
Expand Down
43 changes: 43 additions & 0 deletions apps/toolkit-app/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { api } from "src/blitz-server"
import GithubProvider from "next-auth/providers/github"
import { NextAuthAdapter, BlitzNextAuthOptions } from "@blitzjs/auth/next-auth"
import db, { User } from "db"
import { Role } from "types"

// Has to be defined separately for `profile` to be correctly typed below
const providers = [
GithubProvider({
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
}),
]

export default api(
NextAuthAdapter({
successRedirectUrl: "/",
errorRedirectUrl: "/error",
providers,
callback: async (user, account, profile, session) => {
console.log("USER SIDE PROFILE_DATA", { user, account, profile })
let newUser: User
try {
newUser = await db.user.findFirstOrThrow({ where: { name: { equals: user.name } } })
} catch (e) {
newUser = await db.user.create({
data: {
email: user.email as string,
name: user.name as string,
role: "USER",
},
})
}
const publicData = {
userId: newUser.id,
role: newUser.role as Role,
source: "github",
}
await session.$create(publicData)
return { redirectUrl: "/" }
},
})
)
8 changes: 7 additions & 1 deletion apps/toolkit-app/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Layout from "src/core/layouts/Layout"
import { useCurrentUser } from "src/users/hooks/useCurrentUser"
import logout from "src/auth/mutations/logout"
import { useMutation } from "@blitzjs/rpc"
import { Routes, BlitzPage } from "@blitzjs/next"
import { BlitzPage } from "@blitzjs/next"
import { Routes } from ".blitz"
import styles from "src/styles/Home.module.css"

/*
Expand Down Expand Up @@ -43,6 +44,11 @@ const UserInfo = () => {
<Link href={Routes.LoginPage()} className={styles.loginButton}>
<strong>Login</strong>
</Link>
<Link href="/api/auth/github/login" passHref>
<a className="button small">
<strong>Sign in with GitHub</strong>
</a>
</Link>
</>
)
}
Expand Down
1 change: 1 addition & 0 deletions packages/blitz-auth/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/server/adapters/next-auth/next-auth
7 changes: 6 additions & 1 deletion packages/blitz-auth/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {BuildConfig} from "unbuild"

const config: BuildConfig = {
entries: ["./src/index-browser", "./src/index-server","./src/server/secure-password"],
entries: [
"./src/index-browser",
"./src/index-server",
"./src/server/secure-password",
"./src/server/adapters/next-auth",
],
externals: ["index-browser.cjs", "index-browser.mjs", "react"],
declaration: true,
rollup: {
Expand Down
1 change: 1 addition & 0 deletions packages/blitz-auth/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/next-auth"
1 change: 1 addition & 0 deletions packages/blitz-auth/next-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./dist/next-auth.cjs")
8 changes: 8 additions & 0 deletions packages/blitz-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@types/b64-lite": "1.3.0",
"@types/cookie-session": "2.0.44",
"@types/oauth": "0.9.1",
"@types/passport": "1.0.7",
"@types/secure-password": "3.1.1",
"b64-lite": "1.4.0",
Expand All @@ -38,18 +39,24 @@
"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",
"supports-color": "8.1.1",
"url": "0.11.0"
},
"peerDependencies": {
"blitz": "2.0.0-beta.23",
"next-auth": "4.18.7",
"secure-password": "4.0.0"
},
"peerDependenciesMeta": {
"secure-password": {
"optional": true
},
"next-auth": {
"optional": true
}
},
"devDependencies": {
Expand All @@ -62,6 +69,7 @@
"@types/react": "18.0.25",
"@types/react-dom": "17.0.14",
"blitz": "2.0.0-beta.23",
"next-auth": "4.18.7",
"react": "18.2.0",
"react-dom": "18.2.0",
"secure-password": "4.0.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
@@ -0,0 +1 @@
export * from "./passport/adapter"
3 changes: 3 additions & 0 deletions packages/blitz-auth/src/server/adapters/next-auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./next-auth/adapter"
export * from "./next-auth/types"
export * from "./next-auth/webpack"

0 comments on commit 6f18cbd

Please sign in to comment.