implement authentication with GitHub (Auth.js)

This commit is contained in:
Michi 2025-10-02 22:02:44 +02:00
parent fd042118ad
commit 807e2dc408
5 changed files with 140 additions and 11 deletions

View file

@ -1,5 +1,6 @@
import { Hono } from 'hono'
import { bearerAuth } from 'hono/bearer-auth'
import { authHandler, initAuthConfig, verifyAuth } from '@hono/auth-js'
import GitHub from '@auth/core/providers/github'
import notification from './routes/notification'
import logbook from './routes/logbook'
import cronRouter from './jobs/cron'
@ -7,10 +8,26 @@ import cronRouter from './jobs/cron'
// create app
const app = new Hono()
// add bearer authentication
const token = 'insecure-token'
// OAuth via Auth.js
app.use('*', initAuthConfig((c) => ({
secret: c.env.AUTH_SECRET,
providers: [
GitHub({
clientId: c.env.GITHUB_ID,
clientSecret: c.env.GITHUB_SECRET,
})
]
})))
app.use('/*', bearerAuth({ token }))
app.use('/auth/*', authHandler())
app.use('/*', verifyAuth())
// example endpoint
app.get('/protected', (c) => {
const auth = c.get('authUser')
return c.json(auth)
})
// define routes & export app
app.route('/notification', notification)