Skip to content

Commit

Permalink
Add docs for protecting the documentation routes
Browse files Browse the repository at this point in the history
When @fastify/swagger-ui was part of @fastify/swagger there used to be documentation for protecting the documentation routes, but that wasn't transferred over when [it was removed](fastify/fastify-swagger@f15bebd#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L689) it seems.

I know I've spent a bit of time searching for how to do this, stumbling upon fastify/fastify-swagger#466 and getting confused when the docs are not there anymore in the current HEAD.

I think it would be beneficial to add this to the fastify-swagger-ui docs since it's a very common use case.
  • Loading branch information
fertolg committed Mar 18, 2023
1 parent 9097f96 commit 6b164d4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,35 @@ await fastify.register(require('@fastify/swagger-ui'), {
})
```

#### Protect your documentation routes

You can protect your documentation by configuring an authentication hook.
Here is an example using the [`@fastify/basic-auth`](https://github.com/fastify/fastify-basic-auth) plugin:

##### Example
```js
const fastify = require('fastify')()

fastify.register(require('@fastify/swagger'))

await fastify.register(require('@fastify/basic-auth'), {
validate (username, password, req, reply, done) {
if (username === 'admin' && password === 'admin') {
done()
} else {
done(new Error('You can not access'))
}
},
authenticate: true
})

await fastify.register(require('@fastify/swagger-ui', {
uiHooks: {
onRequest: fastify.basicAuth
}
})
```
<a name="license"></a>
## License
Expand Down

0 comments on commit 6b164d4

Please sign in to comment.