{"id":14989870,"url":"https://github.com/charlesread/fastify-jwt-webapp","last_synced_at":"2025-10-24T19:31:20.155Z","repository":{"id":32916503,"uuid":"146059398","full_name":"charlesread/fastify-jwt-webapp","owner":"charlesread","description":"JWT authentication for fastify-based web apps","archived":false,"fork":false,"pushed_at":"2022-12-22T10:38:22.000Z","size":795,"stargazers_count":11,"open_issues_count":11,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-14T20:50:40.391Z","etag":null,"topics":["fastify","fastify-plugin","jwt"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/charlesread.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-25T02:53:41.000Z","updated_at":"2023-08-31T02:07:42.000Z","dependencies_parsed_at":"2023-01-14T22:39:27.774Z","dependency_job_id":null,"html_url":"https://github.com/charlesread/fastify-jwt-webapp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/charlesread/fastify-jwt-webapp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesread%2Ffastify-jwt-webapp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesread%2Ffastify-jwt-webapp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesread%2Ffastify-jwt-webapp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesread%2Ffastify-jwt-webapp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/charlesread","download_url":"https://codeload.github.com/charlesread/fastify-jwt-webapp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesread%2Ffastify-jwt-webapp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259712380,"owners_count":22900036,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastify","fastify-plugin","jwt"],"created_at":"2024-09-24T14:19:03.310Z","updated_at":"2025-10-24T19:31:15.118Z","avatar_url":"https://github.com/charlesread.png","language":"JavaScript","readme":"[![Build Status](https://travis-ci.org/charlesread/fastify-jwt-webapp.svg?branch=master)](https://travis-ci.org/charlesread/fastify-jwt-webapp)\n[![Coverage Status](https://coveralls.io/repos/github/charlesread/fastify-jwt-webapp/badge.svg?branch=master)](https://coveralls.io/github/charlesread/fastify-jwt-webapp?branch=master)\n\n# fastify-jwt-webapp  \n  \n_fastify-jwt-webapp_ brings the security and simplicity of JSON Web Tokens to your [fastify][fastify]-based web apps, single- and multi-paged \"traditional\" applications are the target of this plugin, although it does not impose a server-side session to accomplish being \"logged-in\" from request to request.  Rather, a JWT is simply stored in a client-side cookie and retrieved and verified with each request after successful login. This plugin does not assume your knowledge of JWTs themselves, but knowledge of the workflows involved, particularly as it relates to *your* provider, are assumed. (this plugin uses a `/authorize -\u003e authorization_code -\u003e /oauth/token -\u003e JWT`-like workflow) \n  \nTo see _fastify-jwt-webapp_ in the wild check out [my website](https://www.charlesread.io).  \n  \n[fastify]: https://fastify.io/\n\n\u003c!-- toc --\u003e\n\n- [Example](#example)\n  * [index.js](#indexjs)\n  * [config.js](#configjs)\n- [Cookie](#cookie)\n- [Refresh Tokens](#refresh-tokens)\n- [JWKS Caching](#jwks-caching)\n- [Options](#options)\n\n\u003c!-- tocstop --\u003e\n\n## Example\n\n```bash  \nnpm install --save fastify-jwt-webapp\n```  \n### index.js \n\n```js\n'use strict'\n\nrequire('pino-pretty')\n\nconst fastify = require('fastify')({\n  https: true,\n  logger: {\n    prettyPrint: true,\n    level: 'trace'\n  }\n})\n\nconst fjwt = require('fastify-jwt-webapp')\n\nconst config = require('./config')\n\nasync function main () {\n  // just local TLS\n  await fastify.register(require('fastify-tls-keygen'))\n  // register the plugin and pass config (from examples/config.js)\n  await fastify.register(fjwt, config.fjwt)\n\n  // a homepage with a login link\n  fastify.get('/', async function (req, reply) {\n    reply\n      .type('text/html')\n      .send('\u003ca href=\"/login\"\u003eClick here to log-in\u003c/a\u003e')\n  })\n\n  // a protected route that will simply display one's credentials\n  fastify.get('/credentials', async function (req, reply) {\n    reply.send({\n      credentials: req.credentials\n    })\n  })\n\n  await fastify.listen(8443, 'localhost')\n}\n\nmain()\n  .then(function() {\n    console.log('server started')\n  })\n  .catch(function (err) {\n    console.error(err.message)\n  })\n``` \n \n### config.js \n\n```js\n'use strict'\n\nconst config = {}\n\nconfig.fjwt = {\n  service: 'auth0',\n  urlAuthorize: 'https://instance.auth0.com/authorize',\n  urlToken: 'https://instance.auth0.com/oauth/token',\n  urlJWKS: 'https://instance.auth0.com/.well-known/jwks.json',\n  client_id: '',\n  client_secret: '',\n  redirect_uri: 'https://localhost:8443/callback',\n  // the following is optional\n  pathSuccessRedirect: '/credentials', // '/' by default\n  pathExempt: [\n    '/',\n    '/login',\n    '/callback'\n  ], // ['/login', '/callback'] by default\n  authorizationCallback: async function (jwtResponse, req, reply) {\n    req.log.info('hello from authorizationCallback!')\n    req.log.info('jwtResponse: %o', jwtResponse)\n  }\n}\n\nmodule.exports = config\n\n```  \n  \n## Cookie  \n  \nBeing \"logged-in\" is achieved by passing along the JWT along with each request, as is typical with APIs (via the `Authorization` header). _fastify-jwt-webapp_ does this by storing the JWT in a cookie (`options.cookie.name`, \"token\" by default).  By default this cookie is `Secure`, meaning that it will only be sent by the browser back to your app if the connection is secure.  To change this behavior set `options.cookie.secure` to `false`.  DO NOT DO THIS IN PRODUCTION.  YOU HAVE BEEN WARNED.  To see cookie options please see `lib/config.js`.\n  \n## Refresh Tokens  \n  \nThis plugin does not treat refresh tokens, but there's no reason that you couldn't implement this functionality yourself. #sorrynotsorry  \n\n## JWKS Caching\n\nFetching the JWKS is by far the most taxing part of this whole process; often making your request 10x slower, that's just the cost of doing business with JWTs in this context because the JWT needs to be verified on each request, and that entails having the public key, thus fetching the JWKS from `options.urlJWKS` on _every single request_.  Fortunately, a JWKS doesn't change particularly frequently, _fastify-jwt-webapp_ can cache the JWKS for `options.cacheJWKSAge` milliseconds, even a value like `10000` (10 seconds) will, in the long-term, add up to much less time spent fetching the JWKS and significantly snappier requests (well, at least until `options.cacheJWKSAge` milliseconds after the caching request, at which point the cache will be refreshed for another `options.cacheJWKSAge` milliseconds).\n  \n## Options  \n  \n| Key |   | Default | Description |\n| --- | --- | --- | --- |\n| `service` | _required_  | `auth0` | This plugin makes use of \"templates\" that control the parameters that are sent to the IdP.  Can be `auth0` or `o365` right now. |\n| `client_id` | _required_ |  | Your client ID. |\n| `client_secret` | _required_ |  | You client secret. |\n| `urlAuthorize` | _required_ |  | The URL that your IdP uses for login, `https://yourinstance.auth0.com/authorize`, for example. |\n| `urlToken` | _required_ |  | The URL that your IdP uses for exchanging an `authorization_code` for access token(s), in this case a JWT, `https://yourinstance.auth0.com/oauth/token`, for example. |\n| `urlJWKS` | _required_ |  | The URL that serves your JWKS, `https://yourinstance.auth0.com/.well-known/jwks.json`, for example. |\n| `cookie.domain` | _required_ | `os.hostname()` | _fastify-jwt-webapp_ works by setting a cookie, so you need to specify the domain for which the cookie will be sent. |\n| `redirect_uri` | _required_ |  | This is the URL to which an IdP should redirect in order to process the successful authentication, `https://myapp.example.com/callback`, for example. |\n| `pathCallback` |  | `/callback` | _fastify-jwt-webapp_ creates several endpoints in your application, this is one of them, it processes the stuff that your IdP sends over after successful authentication, by default the endpoint is `/callback`, but you can change that with this parameter.  This is very related to the `redirect_uri` option mentioned above. |\n| `pathLogin` |  | `/login` | This is the second endpoint that _fastify-jwt-webapp_ adds, it redirects to `urlAuthorize` (with some other stuff along the way), it's `/login` by default, but you can change it to anything, it's just aesthetic. |\n| `pathSuccessRedirect` |  | `/` | Where do you get redirected after successful authentication?  `pathSuccessRedirect`, that's where. |\n| `pathExempt` |   | `['/login', '/callback']` | An array of endpoint paths to be excluded from the actions of the plugin (unauthenticated routes). |\n| `nameCredentialsDecorator` |  | `credentials` | After successful authentication, the fastify request object will be decorated with the payload of the JWT, you can control that decorator here, `req.theLoggedInUsersInfo` for example. |\n| `authorizationCallback` |  |  | `authorizationCallback` is a totally optional function with signature `async function(jwtResponse, request, reply)` that is called after successful authentication, it has absolutely no effect on the plugin's actual functionality. |\n| `cacheJWKSAge` | _(disabled)_ |  | Will cache the JWKS for `cacheJWKSAge` milliseconds after the first request that needs it.|\n| `redirectOnFail` |  | `false` | If set to `true` the plugin will redirect to `pathLogin` if a JWT is present, but not valid. |","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharlesread%2Ffastify-jwt-webapp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcharlesread%2Ffastify-jwt-webapp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharlesread%2Ffastify-jwt-webapp/lists"}