Skip to content

Commit

Permalink
docs(readme): add missing extractToken option (#324)
Browse files Browse the repository at this point in the history
* docs: Missing extractToken option

* fix: typo in doc
  • Loading branch information
dancastillo authored Dec 29, 2023
1 parent 55b7fe6 commit 7a8802f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,36 @@ fastify.post('/sign/:namespace', async function (request, reply) {
})
```

### `extractToken`

Setting this option will allow you to extract a token using function passed in for `extractToken` option. The function definition should be `(request: FastifyRequest) => token`. Fastify JWT will check if this option is set, if this option is set it will use the function defined in the option. When this option is not set then it will follow the normal flow.

```js
const fastify = require('fastify')
const jwt = require('@fastify/jwt')

fastify.register(jwt, { secret: 'test', verify: { extractToken: (request) => request.headers.customauthheader } })

fastify.post('/sign', function (request, reply) {
return reply.jwtSign(request.body)
.then(function (token) {
return { token }
})
})

fastify.get('/verify', function (request, reply) {
// token avaiable via `request.headers.customauthheader` as defined in fastify.register above
return request.jwtVerify()
.then(function (decodedToken) {
return reply.send(decodedToken)
})
})

fastify.listen(3000, function (err) {
if (err) throw err
})
```

#### Typescript

To simplify the use of namespaces in TypeScript you can use the `FastifyJwtNamespace` helper type:
Expand Down

0 comments on commit 7a8802f

Please sign in to comment.