{"id":26593763,"url":"https://github.com/lughino/passport-unique-token","last_synced_at":"2025-03-23T15:20:26.087Z","repository":{"id":17511117,"uuid":"20299042","full_name":"lughino/passport-unique-token","owner":"lughino","description":"Unique token authentication strategy for Passport","archived":false,"fork":false,"pushed_at":"2024-03-27T02:03:41.000Z","size":893,"stargazers_count":38,"open_issues_count":22,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-15T07:27:18.746Z","etag":null,"topics":["authentication-strategy","express","passport","passport-strategy","passport-unique-token"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"dxsdyhm/GwellDemo","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lughino.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-05-29T15:47:14.000Z","updated_at":"2024-06-18T17:06:55.213Z","dependencies_parsed_at":"2024-06-18T17:06:40.236Z","dependency_job_id":"2cb5936a-c861-4050-a57b-c333ebacb86d","html_url":"https://github.com/lughino/passport-unique-token","commit_stats":{"total_commits":121,"total_committers":9,"mean_commits":"13.444444444444445","dds":0.5041322314049587,"last_synced_commit":"067f5e6a322c1672008cebf53cc6c44928003862"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lughino%2Fpassport-unique-token","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lughino%2Fpassport-unique-token/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lughino%2Fpassport-unique-token/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lughino%2Fpassport-unique-token/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lughino","download_url":"https://codeload.github.com/lughino/passport-unique-token/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244988382,"owners_count":20543352,"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-strategy","express","passport","passport-strategy","passport-unique-token"],"created_at":"2025-03-23T15:20:25.200Z","updated_at":"2025-03-23T15:20:26.003Z","avatar_url":"https://github.com/lughino.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Passport Unique Token Strategy\n\n[![CircleCI](https://circleci.com/gh/lughino/passport-unique-token.svg?style=svg)](https://circleci.com/gh/lughino/passport-unique-token)\n[![Maintainability](https://api.codeclimate.com/v1/badges/6cbbdc635903006b578e/maintainability)](https://codeclimate.com/github/lughino/passport-unique-token/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/6cbbdc635903006b578e/test_coverage)](https://codeclimate.com/github/lughino/passport-unique-token/test_coverage)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/a2ec6c1319b6497494303ee024e45cb5)](https://www.codacy.com/app/lughino/passport-unique-token?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=lughino/passport-unique-token\u0026utm_campaign=Badge_Grade)\n![npm](https://img.shields.io/npm/dm/passport-unique-token)\n![npm](https://img.shields.io/npm/v/passport-unique-token)\n![GitHub](https://img.shields.io/github/license/lughino/passport-unique-token)\n[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)\n[![Known Vulnerabilities](https://snyk.io//test/github/lughino/passport-unique-token/badge.svg?targetFile=package.json)](https://snyk.io//test/github/lughino/passport-unique-token?targetFile=package.json)\n\nUnique token authentication strategy for Passport.\n\n## Installation\n\n```sh\nnpm install passport-unique-token\n```\n\n## Usage\n\nThe unique token authentication strategy authenticates users with a unique token.\nThe strategy requires a _verify_ callback,\nwhich accepts these credentials and calls done providing a user.\n\n```javascript\nconst { UniqueTokenStrategy } = require('passport-unique-token');\n\npassport.use(\n  new UniqueTokenStrategy((token, done) =\u003e {\n    User.findOne(\n      {\n        uniqueToken: token,\n        expireToken: { $gt: Date.now() },\n      },\n      (err, user) =\u003e {\n        if (err) {\n          return done(err);\n        }\n\n        if (!user) {\n          return done(null, false);\n        }\n\n        return done(null, user);\n      },\n    );\n  }),\n);\n```\n\nBy default `passport-unique-token` checks for `token` key credentials\nin either the params url or request body in these locations:\n\n| Type   | Default property |\n| ------ | :--------------: |\n| Url    |      token       |\n| Body   |      token       |\n| Query  |      token       |\n| Header |      token       |\n\n### Configure\n\nThese credential locations can be configured when defining the strategy as follows:\n\n```javascript\nconst { UniqueTokenStrategy } = require('passport-unique-token');\nconst strategyOptions = {\n  tokenQuery: 'custom-token',\n  tokenParams: 'custom-token',\n  tokenField: 'custom-token',\n  tokenHeader: 'custom-token',\n  failOnMissing: false\n};\n\npassport.use(new UniqueTokenStrategy(strategyOptions,\n  (token, done) =\u003e {\n    User.findOne({\n      uniqueToken: token,\n      expireToken: { $gt: Date.now() }\n    }, (err, user) =\u003e {\n      if (err) {\n        return done(err);\n      }\n\n      if (!user) {\n        return done(null, false);\n      }\n\n      return done(null, user);\n    });\n  }\n```\n\n`failOnMissing` option allows you to queue multiple strategy, customizing the behavior.\nBy default it's set to `true`, when it's set to `false`\nit lets move on to the next strategy on failure.\n\n## How to Authenticate\n\nUse `passport.authenticate()`, specifying the `token` strategy to authenticate requests.\n\nFor example, as route middleware in an [Express](http://expressjs.com/) application:\n\n```javascript\napp.put('/animals/dogs', passport.authenticate('token'), (req, res) =\u003e {\n  // User authenticated and can be found in req.user\n});\n```\n\nIf authentication fails in the above example then a `401` response will be given.\nHowever there may be times you wish a bit more control and delegate\nthe failure to your application:\n\n```javascript\napp.put('/animals/dogs', authenticate, (req, res) =\u003e {\n  // User authenticated and can be found in req.user\n});\n\nfunction authenticate(req, res, next) {\n  passport.authenticate('token', (err, user, info) =\u003e {\n    if (err) {\n      return next(err);\n    }\n\n    if (!user) {\n      res.status(401).json({ message: 'Incorrect token credentials' });\n    }\n\n    req.user = user;\n    next();\n  })(req, res, next);\n}\n```\n\n## Api Reference\n\n- [`UniqueTokenStrategy()`](#UniqueTokenStrategy)\n- [`authenticate()`](#authenticate)\n\n### `UniqueTokenStrategy()`\n\nThe token authentication strategy authenticates requests based on the credentials\nsubmitted through standard request headers, body, querystring or params.\n\n```typescript\nnew UniqueTokenStrategy(\n  options?: {\n    // the token field name in the body request\n    tokenField?: string = 'token',\n    // the token field name in the query string request\n    tokenQuery?: string = 'token',\n    // the token field name in the param request\n    tokenParams?: string = 'token',\n    // the token field name in the header request\n    tokenHeader?: string = 'token',\n    // if `true` the express.Request is the first parameter of the verify callback\n    passReqToCallback?: false,\n    // if `true` the token key is case sensitive (e.g. res.body['uniqueToken'])\n    caseSensitive?: false,\n    // allows you to queue multiple strategy, customizing the behavior.\n    failOnMissing?: true\n  },\n  verify: (\n    req?: express.Request,\n    token: string,\n    done: (err: Error | null, user?: any, info?: any) =\u003e void\n  ) =\u003e void\n)\n```\n\n### `authenticate()`\n\nYou can optionally pass options to the `authenticate()` method.\nPlease refer to the [passport documentation](http://www.passportjs.org/docs/authenticate/)\nfor the different signature.\n\n```typescript\nauthenticate(\n  strategyName: string,\n  options?: { badRequestMessage: string },\n  callback?: { err: Error, user: any, info: any }\n);\n\n// Example:\n\napp.post('/login', passport.authenticate('token', {\n  badRequestMessage: 'custom error message'\n}));\n```\n\n## Credits\n\n[Luca Pau](http://github.com/lughino)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flughino%2Fpassport-unique-token","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flughino%2Fpassport-unique-token","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flughino%2Fpassport-unique-token/lists"}