{"id":14989343,"url":"https://github.com/chscript/koa-rejwt","last_synced_at":"2026-01-04T22:33:11.631Z","repository":{"id":152506618,"uuid":"609859505","full_name":"chscript/koa-rejwt","owner":"chscript","description":"⚙️ A koa middleware that supports both token validation and refresh.","archived":false,"fork":false,"pushed_at":"2023-04-11T04:45:13.000Z","size":56,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-26T18:51:36.230Z","etag":null,"topics":["jsonwebtoken","koa","middleware"],"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/chscript.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-05T13:09:51.000Z","updated_at":"2023-04-11T04:49:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"7ba80a18-85e5-4616-aba9-37aa533657d3","html_url":"https://github.com/chscript/koa-rejwt","commit_stats":{"total_commits":1,"total_committers":1,"mean_commits":1.0,"dds":0.0,"last_synced_commit":"ffa61bc4b80f76f2d28f2401748f0bb0b1ffa476"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chscript%2Fkoa-rejwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chscript%2Fkoa-rejwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chscript%2Fkoa-rejwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chscript%2Fkoa-rejwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chscript","download_url":"https://codeload.github.com/chscript/koa-rejwt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244868753,"owners_count":20523591,"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":["jsonwebtoken","koa","middleware"],"created_at":"2024-09-24T14:18:09.862Z","updated_at":"2026-01-04T22:33:11.591Z","avatar_url":"https://github.com/chscript.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003ekoa-rejwt\u003c/h1\u003e\n\n\u003cp align=\"center\" \u003e\n\u003cimg  src=\"https://img.shields.io/badge/build-passing-brightgreen\" /\u003e\n\u003cimg  src=\"https://img.shields.io/badge/npm-v0.1.0-blue\" /\u003e\n\u003cimg  src=\"https://img.shields.io/badge/License-MIT-green\" /\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/chscript/koa-rejwt/blob/main/README_zh.md\"\u003e中文\u003c/a\u003e｜\n  \u003ca href=\"https://github.com/chscript/koa-rejwt/blob/main/README.md\"\u003eEnglish\u003c/a\u003e\u003c/p\u003e\n\n\n## Introduction\n\nkoa-rejwt is a Koa middleware that supports both token validation and refresh.\n\n## Install\n\n```shell\nnpm install koa-rejwt\n```\n\n## Option\n\n`secret: Secret | GetSecret` (required): Verify key.\n\n`reftime?: string | number` (optional): Token refresh time. Greater than zero and must be less than the expiration time of the initial token. If a user initiates a new request after the refresh time, the response header will carry the new token and send it to the client. Developers can set the response interceptor on the client side to obtain the refreshed token in the `Authorization` field of the response header.\n\n`signSecret?: Secret | GetSecret` (optional): Signature key.\n\n`signOptions?: SignOptions` (optional): Signature options.\n\n`getToken?: GetToken` (optional): A function that receives the express `Request` and returns the token, by default it looks in the `Authorization` header.\n\n`isRevoked?: IsRevoked` (optional): A function used to verify whether a token has been revoked.\n\n`onExpired?: OnExpired` (optional): A function used to handle expired tokens.\n\n`stateObjKey?: string` (optional): The attribute name of the payload stored in the `ctx. state` object. The default value is `payload`.\n\n`credentialsRequired?: boolean` (optional): Whether to use a certificate. The default value is `true`.\n\n## Usage \u0026 Example\n\n**client**：Set request interceptors and response interceptors through Axios to verify locally stored tokens and receive refreshed tokens, respectively.\n\n```typescript\naxios.interceptors.request.use( // 请求拦截器\n    config =\u003e {\n        if (httpConfig.useToken) { // 发送本地存储的token到服务端\n            config.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token')\n        }\n        return config\n    },\n    error =\u003e {\n        return Promise.reject(error)\n    }\n)\naxios.interceptors.response.use( // 响应拦截器\n    res =\u003e {\n        if (!!res.headers['Authorization']) { // 接收来自服务端刷新的token\n            localStorage.setItem('token', res.headers['authorization'])\n        }\n        return res.data\n    },\n    error =\u003e {\n        return Promise.reject(error)\n    }\n)\n```\n\n**server**：Set the koa rejwt middleware in the Koa instance object `server`, while also configuring the token's verification key and refresh time. If the client sends a request to carry a token after setting the token refresh time but before reaching the expiration time, it can receive the refreshed token in the response interceptor set by the client and save it to the `localStorage` object.\n\n```javascript\nconst Koa = require('koa')\nconst rejwt = require('koa-rejwt')\n\nconst server = new Koa()\nserver.use(\n    rejwt({ secret: 'shared-secret', reftime: '2days' }).unless({\n        path: ['/login']\n    })) // 其中 reftime 必须小于token过期时间且不为0\nserver.listen(3000)\n```\n\n## License\n\n[MIT](https://github.com/chscript/koa-rejwt/blob/main/LICENSE)\n\nCopyright (c) 2023-present Steve Yang","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchscript%2Fkoa-rejwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchscript%2Fkoa-rejwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchscript%2Fkoa-rejwt/lists"}