Skip to content

Commit 7a8802f

Browse files
authored
docs(readme): add missing extractToken option (#324)
* docs: Missing extractToken option * fix: typo in doc
1 parent 55b7fe6 commit 7a8802f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,36 @@ fastify.post('/sign/:namespace', async function (request, reply) {
469469
})
470470
```
471471

472+
### `extractToken`
473+
474+
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.
475+
476+
```js
477+
const fastify = require('fastify')
478+
const jwt = require('@fastify/jwt')
479+
480+
fastify.register(jwt, { secret: 'test', verify: { extractToken: (request) => request.headers.customauthheader } })
481+
482+
fastify.post('/sign', function (request, reply) {
483+
return reply.jwtSign(request.body)
484+
.then(function (token) {
485+
return { token }
486+
})
487+
})
488+
489+
fastify.get('/verify', function (request, reply) {
490+
// token avaiable via `request.headers.customauthheader` as defined in fastify.register above
491+
return request.jwtVerify()
492+
.then(function (decodedToken) {
493+
return reply.send(decodedToken)
494+
})
495+
})
496+
497+
fastify.listen(3000, function (err) {
498+
if (err) throw err
499+
})
500+
```
501+
472502
#### Typescript
473503

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

0 commit comments

Comments
 (0)