Skip to content

Commit

Permalink
register @fastify/formbody in plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hillac committed Mar 26, 2024
1 parent 0b38522 commit 841ef7b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/frameworks-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"build": "pnpm clean && pnpm providers && tsc",
"clean": "rm -rf lib index.* src/lib/providers",
"test": "vitest run -c ../utils/vitest.config.ts",
"test:debug": "NODE_OPTIONS=\"--inspect --inspect-brk\" vitest run -c ../utils/vitest.config.ts",
"test:debug": "vitest --inspect --inspect-brk --pool threads --poolOptions.threads.singleThread run -c ../utils/vitest.config.ts",
"providers": "node ../utils/scripts/providers"
},
"publishConfig": {
Expand All @@ -46,6 +46,7 @@
"fastify": "^4.25.2"
},
"peerDependencies": {
"@fastify/formbody": "^7.4.0",
"fastify": "^4.25.2"
},
"keywords": [
Expand Down
11 changes: 6 additions & 5 deletions packages/frameworks-fastify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
*
* ```ts title="src/routes/auth.route.ts"
* import { FastifyAuth } from "@auth/fastify"
* import formbodyParser from "@fastify/formbody"
* import GitHub from "@auth/fastify/providers/github"
* import Fastify from "fastify"
*
* // If app is served through a proxy, trust the proxy to allow HTTPS protocol to be detected
* const fastify = Fastify({ trustProxy: true });
*
* // Make sure to use a form body parser so Auth.js can receive data from the client
* fastify.register(formbodyParser)
*
* fastify.register(FastifyAuth({ providers: [ GitHub ] }), { prefix: '/auth' })
* ```
*
Expand All @@ -51,6 +47,7 @@
* import { getSession } from "@auth/fastify"
*
* // Decorating the reply is not required but will optimise performance
* // Only decorate the reply with a value type like null, as reference types like objects are shared among all requests, creating a security risk.
* fastify.decorateReply('session', null)
* export async function authSession(req: FastifyRequest, reply: FastifyReply) {
Expand Down Expand Up @@ -136,6 +133,7 @@
import { Auth, setEnvDefaults, createActionURL } from "@auth/core"
import type { AuthConfig, Session } from "@auth/core/types"
import type { FastifyRequest, FastifyPluginAsync } from 'fastify'
import formbody from '@fastify/formbody'
import { toWebRequest, toFastifyReply } from "./lib/index.js"

export type {
Expand All @@ -148,7 +146,10 @@ export type {

export function FastifyAuth(config: Omit<AuthConfig, "raw">): FastifyPluginAsync {
setEnvDefaults(process.env, config)
return async (fastify, opts) => {
return async (fastify) => {
if (!fastify.hasContentTypeParser('application/x-www-form-urlencoded')) {
fastify.register(formbody)
}
fastify.route({
method: ['GET', 'POST'],
url: '/*',
Expand Down
8 changes: 6 additions & 2 deletions packages/frameworks-fastify/tests/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ describe("Integration test with login and getSession", () => {
it("Should return the session with username after logging in", async () => {
let expectations: Function = () => {}

fastify.register(formbodyParser)

fastify.register(FastifyAuth(authConfig), { prefix: "/api/auth" })

fastify.post("/test", async (request, reply) => {
Expand Down Expand Up @@ -123,4 +121,10 @@ describe("Integration test with login and getSession", () => {
const expectationResult = await expectations()
expect(expectationResult).toEqual(true)
})

it("Should not throw when form body parser already registered", async () => {
fastify.register(formbodyParser)
fastify.register(FastifyAuth(authConfig), { prefix: "/api/auth" })
await fastify.ready()
})
})

0 comments on commit 841ef7b

Please sign in to comment.