Skip to content

Commit

Permalink
added next-auth middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Dec 23, 2023
1 parent 616afbc commit 128caa6
Show file tree
Hide file tree
Showing 13 changed files with 2,593 additions and 4,242 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-lies-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/next-auth': major
---

initial support for next auth
25 changes: 25 additions & 0 deletions .github/workflows/ci-next-auth.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: ci-next-auth
on:
push:
branches: [main]
paths:
- 'packages/next-auth/**'
pull_request:
branches: ['*']
paths:
- 'packages/next-auth/**'

jobs:
ci:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./packages/next-auth
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18.x
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"build:esbuild-transpiler": "yarn workspace @hono/esbuild-transpiler build",
"build:oauth-providers": "yarn workspace @hono/oauth-providers build",
"build:react-renderer": "yarn workspace @hono/react-renderer build",
"build:next-auth": "yarn workspace @hono/next-auth build",
"build": "run-p 'build:*'",
"lint": "eslint 'packages/**/*.{ts,tsx}'",
"lint:fix": "eslint --fix 'packages/**/*.{ts,tsx}'",
Expand Down
74 changes: 74 additions & 0 deletions packages/next-auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Next Auth middleware for Hono

This is a [Next Auth](https://next-auth.js.org) third-party middleware for [Hono](https://github.com/honojs/hono).

This middleware can be used to inject the Next Auth session into the request context.

## Installation

```plain
npm i hono @hono/next-auth @auth/core
```

## Configuration

Before starting using the middleware you must set the following environment variables:

```plain
AUTH_URL=Same as NEXT_URL
AUTH_SECRET= Same as NEXT_AUTH_SECRET
```

## How to Use

```ts
import { Hono ,Context} from 'hono'
import { authHandler, initAuthConfig, verifyAuth, AuthConfig } from "@hono/next-auth"

const app = new Hono()

app.use("/api/*", initAuthConfig(getAuthConfig))

app.use("/api/auth/*", authHandler())


// This middleware will return with 401 status if auth is invalid
app.use('/api/protected', verifyAuth())

//If auth is valid
app.get('/api/protected', (c) => {
const auth = c.get("nextAuthUser")
return c.json(auth)
})

function getAuthConfig(c: Context): AuthConfig {
return {
secret: c.env.AUTH_SECRET,
authUrl: c.env.AUTH_URL,
providers: [
GitHub({
clientId: c.env.GITHUB_ID,
clientSecret: c.env.GITHUB_SECRET,
authorization: {
params: { scope: "read:user user:email repo" },
},
}),
],
callbacks: {
jwt({ token, account, profile }) {

},
async signIn({ user, account, profile, email, credentials }) {

return true
},
}
}
}

export default app
```
**For React just import Next auth function from @hono/next-auth/react**
## Author

Divyam <https://github.com/divyam234>
64 changes: 64 additions & 0 deletions packages/next-auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@hono/next-auth",
"version": "1.0.0",
"description": "A third-party Next auth middleware for Hono",
"main": "dist/index.js",
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"./react": {
"import": {
"types": "./dist/react.d.mts",
"default": "./dist/react.mjs"
},
"require": {
"types": "./dist/react.d.ts",
"default": "./dist/react.js"
}
}
},
"files": [
"dist"
],
"scripts": {
"test": "vitest --run",
"build": "tsup",
"prerelease": "yarn build && yarn test",
"release": "yarn publish"
},
"license": "MIT",
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/honojs/middleware.git"
},
"homepage": "https://github.com/honojs/middleware",
"peerDependencies": {
"@auth/core": "0.*",
"hono": "3.*"
},
"devDependencies": {
"@auth/core": "^0.19.0",
"@types/react": "^18",
"hono": "^3.11.7",
"jest": "^29.7.0",
"react": "^18.2.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vitest": "^1.0.4"
},
"engines": {
"node": ">=18.4.0"
}
}
Loading

0 comments on commit 128caa6

Please sign in to comment.