forked from honojs/middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
2,593 additions
and
4,242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hono/next-auth': major | ||
--- | ||
|
||
initial support for next auth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.