{"id":14989869,"url":"https://github.com/kriasoft/jwt-passport","last_synced_at":"2025-04-12T01:41:13.211Z","repository":{"id":49124606,"uuid":"126644448","full_name":"kriasoft/jwt-passport","owner":"kriasoft","description":"Passport.js framework that uses JWT for sessions","archived":false,"fork":false,"pushed_at":"2021-06-28T02:22:12.000Z","size":126,"stargazers_count":7,"open_issues_count":10,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-30T00:55:25.296Z","etag":null,"topics":["authentication","jsonwebtoken","jwt","jwt-auth","jwt-authentication","jwt-token","login","passport","passport-strategy","passportjs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/jwt-passport","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/kriasoft.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-03-24T22:12:44.000Z","updated_at":"2023-09-19T15:04:39.000Z","dependencies_parsed_at":"2022-09-05T03:00:34.050Z","dependency_job_id":null,"html_url":"https://github.com/kriasoft/jwt-passport","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kriasoft%2Fjwt-passport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kriasoft%2Fjwt-passport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kriasoft%2Fjwt-passport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kriasoft%2Fjwt-passport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kriasoft","download_url":"https://codeload.github.com/kriasoft/jwt-passport/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601496,"owners_count":20964872,"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":["authentication","jsonwebtoken","jwt","jwt-auth","jwt-authentication","jwt-token","login","passport","passport-strategy","passportjs"],"created_at":"2024-09-24T14:19:03.237Z","updated_at":"2025-04-12T01:41:13.192Z","avatar_url":"https://github.com/kriasoft.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Passport.js framework that uses JWT for sessions\n\n\u003e This is an alternative framework for Passport.js that is designed to use JWT\n\u003e tokens for sessions. So that, instead of storing user's ID and metadata in a\n\u003e database (e.g. Redis), it encodes that data into a JSON Web Token and writes\n\u003e that token to a session cookie.\n\n## How to Install\n\n```bash\n$ npm install jwt-passport\n```\n\n_**Note**: It requires Node.js 6.11 or higher_\n\n## How to Use\n\n```js\nconst uuid = require('uuid');\nconst express = require('express');\nconst passport = require('passport');\nconst jwt = require('jwt-passport');\n\n// We're using Knex.js database client in this examle,\n// but it could be any other database driver.\nconst db = require('./db');\n\npassport.framework(\n  jwt({\n    name: '__session',\n    secret: '\u003csecret\u003e',\n    audience: '\u003caudience\u003e',\n    issuer: '\u003cissuer\u003e',\n    expiresIn: '1 hour',\n\n    // Prepare payload for an ID token\n    createToken: req =\u003e ({\n      sub: req.user.id,\n      jti: uuid.v4(),\n    }),\n\n    // Save user's token in a database\n    saveToken: token =\u003e\n      db\n        .table('user_tokens')\n        .insert({\n          user_id: token.sub,\n          token_id: token.jti,\n        }),\n\n    // Revoke user's token\n    deleteToken: token =\u003e\n      db\n        .table('user_tokens')\n        .where({ token_id: token.jti })\n        .del(),\n\n    // Check if the token was not revoked and find the corresponding user\n    findUser: token =\u003e\n      db\n        .table('user_tokens')\n        .leftJoin('users', 'users.id', 'user_tokens.user_id')\n        .where({ 'user_tokens.token_id': token.jti })\n        .select('users.*')\n        .first(),\n  });\n);\n\npassport.use(new FacebookStrategy(/* config */));\npassport.use(new TwitterStrategy(/* config */));\n\nconst app = express();\n\n// Extend the HTTP request object with\n// req.logIn() and req.logOut() helper methods\napp.use(passport.initialize());\n\n// Attemp to parse session cookie, validate the token\n// and put the authenticated user object onto the contxt (req.user)\napp.use(passport.session());\n\napp.get('/', (req, res) =\u003e {\n  res.send(`Welcome, ${req.user ? req.user.displayName : 'guest'}!`);\n});\n\napp.get('/login/:provider', (req, res, next) =\u003e {\n  passport.authenticate(req.params.provider, /* options */)(req, res, next);\n});\n\napp.get('/login/:provider/return', (req, res, next) =\u003e {\n  passport.authenticate(req.params.provider, /* options */)(req, res, next);\n});\n```\n\n## Related Projects\n\n* [Passport.js][passport] — Simple, unobtrusive authentication for Node.js.\n* [Node.js API Starter][nsk] — Boilerplate for authoring GraphQL APIs with Node.js and PostgreSQL\n* [React Starter Kit][rsk] — Boilerpalte for authoring isomorphic web apps with React.js and GraphQL\n* [React Starter Kit for Firebase][rskfb] — React.js web app boilerplate for serveless architecture\n\n## License\n\nCopyright © 2018-present Kriasoft. This source code is licensed under the MIT\n[license][lic].\n\n[passport]: https://github.com/jaredhanson/passport\n[nsk]: https://github.com/kriasoft/nodejs-api-starter\n[rsk]: https://github.com/kriasoft/react-starter-kit\n[rskfb]: https://github.com/kriasoft/react-firebase-starter\n[lic]: https://github.com/kriasoft/jwt-passport/blob/master/LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkriasoft%2Fjwt-passport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkriasoft%2Fjwt-passport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkriasoft%2Fjwt-passport/lists"}