2023-12-29 03:30:25 +08:00
|
|
|
# Auth.js middleware for Hono
|
|
|
|
|
|
|
|
This is a [Auth.js](https://authjs.dev) third-party middleware for [Hono](https://github.com/honojs/hono).
|
|
|
|
|
|
|
|
This middleware can be used to inject the Auth.js session into the request context.
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
```plain
|
|
|
|
npm i hono @hono/auth-js @auth/core
|
|
|
|
```
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
Before starting using the middleware you must set the following environment variables:
|
|
|
|
|
|
|
|
```plain
|
|
|
|
AUTH_SECRET=#required
|
2024-10-21 06:23:51 +08:00
|
|
|
AUTH_URL=https://example.com/api/auth
|
2023-12-29 03:30:25 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
## How to Use
|
|
|
|
|
|
|
|
```ts
|
2024-10-21 06:23:51 +08:00
|
|
|
import { Hono } from 'hono'
|
|
|
|
import { authHandler, initAuthConfig, verifyAuth } from '@hono/auth-js'
|
|
|
|
import GitHub from '@auth/core/providers/github'
|
2023-12-29 03:30:25 +08:00
|
|
|
|
|
|
|
const app = new Hono()
|
|
|
|
|
2024-10-21 06:23:51 +08:00
|
|
|
app.use(
|
|
|
|
'*',
|
|
|
|
initAuthConfig((c) => ({
|
|
|
|
secret: c.env.AUTH_SECRET,
|
|
|
|
providers: [
|
|
|
|
GitHub({
|
|
|
|
clientId: c.env.GITHUB_ID,
|
|
|
|
clientSecret: c.env.GITHUB_SECRET,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}))
|
|
|
|
)
|
2023-12-29 03:30:25 +08:00
|
|
|
|
2024-10-21 06:23:51 +08:00
|
|
|
app.use('/api/auth/*', authHandler())
|
2023-12-29 03:30:25 +08:00
|
|
|
|
|
|
|
app.use('/api/*', verifyAuth())
|
|
|
|
|
|
|
|
app.get('/api/protected', (c) => {
|
2024-10-21 06:23:51 +08:00
|
|
|
const auth = c.get('authUser')
|
2023-12-29 03:30:25 +08:00
|
|
|
return c.json(auth)
|
|
|
|
})
|
|
|
|
|
|
|
|
export default app
|
|
|
|
```
|
|
|
|
|
|
|
|
React component
|
|
|
|
|
2024-10-21 06:23:51 +08:00
|
|
|
```tsx
|
|
|
|
import { SessionProvider, useSession } from '@hono/auth-js/react'
|
2023-12-29 03:30:25 +08:00
|
|
|
|
2024-10-21 06:23:51 +08:00
|
|
|
export default function App() {
|
2023-12-29 03:30:25 +08:00
|
|
|
return (
|
|
|
|
<SessionProvider>
|
2024-10-21 06:23:51 +08:00
|
|
|
<Children />
|
2023-12-29 03:30:25 +08:00
|
|
|
</SessionProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function Children() {
|
|
|
|
const { data: session, status } = useSession()
|
2024-10-21 06:23:51 +08:00
|
|
|
return <div>I am {session?.user}</div>
|
2023-12-29 03:30:25 +08:00
|
|
|
}
|
|
|
|
```
|
2024-10-21 06:23:51 +08:00
|
|
|
|
2023-12-29 03:30:25 +08:00
|
|
|
Default `/api/auth` path can be changed to something else but that will also require you to change path in react app.
|
|
|
|
|
|
|
|
```tsx
|
2024-10-21 06:23:51 +08:00
|
|
|
import { SessionProvider, authConfigManager, useSession } from '@hono/auth-js/react'
|
2023-12-29 03:30:25 +08:00
|
|
|
|
|
|
|
authConfigManager.setConfig({
|
|
|
|
basePath: '/custom', // if auth route is diff from /api/auth
|
2024-10-21 06:23:51 +08:00
|
|
|
})
|
2023-12-29 03:30:25 +08:00
|
|
|
|
2024-10-21 06:23:51 +08:00
|
|
|
export default function App() {
|
2023-12-29 03:30:25 +08:00
|
|
|
return (
|
|
|
|
<SessionProvider>
|
|
|
|
<Children />
|
|
|
|
</SessionProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function Children() {
|
|
|
|
const { data: session, status } = useSession()
|
2024-10-21 06:23:51 +08:00
|
|
|
return <div>I am {session?.user}</div>
|
2023-12-29 03:30:25 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-10-21 06:23:51 +08:00
|
|
|
SessionProvider is not needed with react query.Use useQuery hook to fetch session data.
|
2023-12-29 03:30:25 +08:00
|
|
|
|
|
|
|
```ts
|
2024-10-21 06:23:51 +08:00
|
|
|
const useSession = () => {
|
|
|
|
const { data, status } = useQuery({
|
|
|
|
queryKey: ['session'],
|
|
|
|
queryFn: async () => {
|
|
|
|
const res = await fetch('/api/auth/session')
|
|
|
|
return res.json()
|
|
|
|
},
|
|
|
|
staleTime: 5 * (60 * 1000),
|
|
|
|
gcTime: 10 * (60 * 1000),
|
|
|
|
refetchOnWindowFocus: true,
|
|
|
|
})
|
|
|
|
return { session: data, status }
|
2023-12-29 03:30:25 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Working example repo https://github.com/divyam234/next-auth-hono-react
|
|
|
|
|
|
|
|
## Author
|
|
|
|
|
|
|
|
Divyam <https://github.com/divyam234>
|