-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (34 loc) · 970 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict'
import fp from 'fastify-plugin'
import { toNodeHandler } from 'better-auth/node'
export function mapHeaders(headers) {
const entries = Object.entries(headers)
const map = new Map()
for (const [headerKey, headerValue] of entries) {
if (headerValue != null) {
map.set(headerKey, headerValue)
}
}
return map
}
async function betterAuthPlugin(fastify, options) {
fastify.decorate('auth', options.auth)
await fastify.register(fastify => {
const authHandler = toNodeHandler(options.auth)
fastify.addContentTypeParser(
'application/json',
/* c8 ignore next 3 */
(_request, _payload, done) => {
done(null, null)
}
)
fastify.all('/api/auth/*', async (request, reply) => {
reply.raw.setHeaders(mapHeaders(reply.getHeaders()))
await authHandler(request.raw, reply.raw)
})
})
}
export default fp(betterAuthPlugin, {
fastify: '5.x',
name: 'fastify-better-auth'
})