{"id":16321589,"url":"https://github.com/hstarorg/express-token","last_synced_at":"2025-07-22T05:34:13.591Z","repository":{"id":75612959,"uuid":"86123564","full_name":"hstarorg/express-token","owner":"hstarorg","description":"Token authentication for express.","archived":false,"fork":false,"pushed_at":"2017-05-03T06:45:41.000Z","size":40,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-09T05:33:57.944Z","etag":null,"topics":["authentication","express","token"],"latest_commit_sha":null,"homepage":null,"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/hstarorg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-03-25T01:19:42.000Z","updated_at":"2019-03-14T00:58:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"8cd2aa43-4384-4fe8-a3b6-9f8fad0aa7ac","html_url":"https://github.com/hstarorg/express-token","commit_stats":{"total_commits":4,"total_committers":2,"mean_commits":2.0,"dds":0.25,"last_synced_commit":"1a7d10fd6fe52ca3ace6db280c21868c81835bca"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/hstarorg/express-token","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hstarorg%2Fexpress-token","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hstarorg%2Fexpress-token/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hstarorg%2Fexpress-token/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hstarorg%2Fexpress-token/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hstarorg","download_url":"https://codeload.github.com/hstarorg/express-token/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hstarorg%2Fexpress-token/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266433807,"owners_count":23927822,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["authentication","express","token"],"created_at":"2024-10-10T22:48:16.603Z","updated_at":"2025-07-22T05:34:13.567Z","avatar_url":"https://github.com/hstarorg.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-token\nToken authentication for express.\n\n前后端分离之后，一般会采用 `rest api` 类似方式来进行数据交互，此时如果用Session认证可能就不是那么合适（当然，也是可以的）。\n\n由于我自己的多个项目都采用了token认证，每次都去实现一套token认证，也是蛮不爽的。so，搞个中间件玩起来。\n\n# How to use\n\n```bash\n# Install\nnpm i --save express-token\n\n# Use\nconst expressToken = require('express-token');\n\napp.use(expressToken(options)); // optional options\n\n# When login\nexpressToken.login(userInfo)\n.then(token =\u003e {\n  // There can get token, you can use it do anything.\n  console.log(token);\n});\n\n# When logout\nexpressToken.logout(token)\n.then(() =\u003e {\n  // Logout succeed，do something...\n});\n\n# Get token or user info\n\nrouter.user('/', (req, res, next) =\u003e {\n  let token = req.token; // Get token\n  let user = req.user; // Get user info\n  if(!req.user){\n    // Not login\n  }\n});\n```\n\n**Options（均为可选参数，默认无需设置）**\n\n* ttl {Number} 单位毫秒，token滑动过期时间，默认30分钟\n* tokenHeader {String} 自定义的token header，默认 'x-token'（决定我们从哪个header获取token）\n* generateToken {(req: Request) =\u003e {}} 生成token的函数，允许返回Promise\n* getToken {(req: Request, tokenHeader: string) =\u003e {}} 获取token的函数，如果配置了该函数，将不会通过tokenHeader获取token。\n\n示例：\n\n```js\nlet options = {\n  ttl: 1000 * 60 * 60, // 1个小时\n  tokenHeader: 'x-test-token', // 默认通过 req.headers['x-test-token'] 获取token\n  generateToken(req){ // 自定义token生成函数\n    return Math.random().toString(16);\n  },\n  getToken(req, tokenHeader){ // 自定义获取token\n    return req.headers['xxx'];\n  }\n};\n```\n\n# How to develop?\n\n```bash\ngit clone\n\n# Run dev\nnpm run dev\n```\n\n# How to feedback?\n\nIf you find some bug or have some feedback, please [create an issue](https://github.com/hstarorg/express-token/issues/new).\n\n# ChANGELOG\n\n[Click here](CHANGELOG.md) to show more change log.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhstarorg%2Fexpress-token","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhstarorg%2Fexpress-token","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhstarorg%2Fexpress-token/lists"}