{"id":41362647,"url":"https://github.com/robophil/sails-hook-jsonwebtoken","last_synced_at":"2026-02-01T19:00:30.594Z","repository":{"id":57151505,"uuid":"71372024","full_name":"robophil/sails-hook-jsonwebtoken","owner":"robophil","description":"A sails hook for easily working with jsonwebtoken https://github.com/auth0/node-jsonwebtoken","archived":false,"fork":false,"pushed_at":"2017-07-06T15:58:52.000Z","size":67,"stargazers_count":11,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-10-26T13:51:54.756Z","etag":null,"topics":["jsonwebtoken","sails-hook","sails-hook-jsonwebtoken","sailsjs"],"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/robophil.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":"2016-10-19T15:32:38.000Z","updated_at":"2019-11-14T21:13:33.000Z","dependencies_parsed_at":"2022-09-06T16:41:03.747Z","dependency_job_id":null,"html_url":"https://github.com/robophil/sails-hook-jsonwebtoken","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/robophil/sails-hook-jsonwebtoken","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robophil%2Fsails-hook-jsonwebtoken","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robophil%2Fsails-hook-jsonwebtoken/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robophil%2Fsails-hook-jsonwebtoken/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robophil%2Fsails-hook-jsonwebtoken/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robophil","download_url":"https://codeload.github.com/robophil/sails-hook-jsonwebtoken/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robophil%2Fsails-hook-jsonwebtoken/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28986317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-01T18:17:03.387Z","status":"ssl_error","status_checked_at":"2026-02-01T18:16:57.287Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["jsonwebtoken","sails-hook","sails-hook-jsonwebtoken","sailsjs"],"created_at":"2026-01-23T08:00:35.462Z","updated_at":"2026-02-01T19:00:30.554Z","avatar_url":"https://github.com/robophil.png","language":"JavaScript","readme":"# sails-hook-jsonwebtoken\n\nA sails hook for easily using jsonwebtoken. It wraps around the popular [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken).\n\n## install\n\n```bash\nnpm install sails-hook-jsonwebtoken --save\n```\n\n1. [Configure sails-hook-jsonwebtoken](#configuration)\n1. [Applying policy for securing routes](#policy)\n1. [Jwt open routes](#jwt-routes-sign-up--sign-in)\n    1. [Signup](#signup)\n    1. [Signin](#signin)\n1. [Accessing a secured route](#accessing-a-secure-route)\n1. [Using the JwtService](#using-the-jwtservice)\n1. [Password reset](#using-the-jwtservice) *see JwtService functions*\n\n\n\n## configuration\n\ncreate config file `config/jsonWebToken.js` and update the defaults to suit your needs\n\n```javascript\nmodule.exports.jsonWebToken = {\n    token_secret: 'i-am-a-secret-token',\n    options:{expiresIn: '2h'}, //see below this section for more on `options`\n    default_account_status: true,\n    afterSignup: function (user) {\n        console.log(\"User account created\")\n    },\n    afterSignin: function (user) {\n        console.log(\"successful login\")\n    },\n    authType: \"email\" //could be {email or username}\n}\n```\n\n\n* **token_secret** - your secret key used for generating token\n* **options** - see here for [options](https://github.com/auth0/node-jsonwebtoken#usage) settings\n* **default_account_status** - status of an account when created, if you need to do any other validation after account has been created set this to `false` then change to `true` when this is done. How you treat user account based on the status of this value is up to you\n* **authType** - This could be `email` or `username`. Depending on your application needs\n* **afterSignup** - This function is called every time a new account is created. The new `user` account created is passed to this function\n* **afterSignin** - This function is called every time someone signs in. The `user` information is passed to the function\n\n\n\n## policy\n\nThere are 3 policies that could be applied to secure your route. They are `JwtPolicy`, `UserIsAdminPolicy` and `UserIsUserPolicy`.\n\n* `JwtPolicy` - Simply checks if the incoming request has the right authorization, the user exists and the token passed to it is still valid.\n\n* `UserIsAdminPolicy` - Does exactly what the `JwtPolicy` does, but also checks if the accountType is of the type **admin**\n\n* `UserIsUserPolicy` - Does exactly what the `JwtPolicy` does, but also checks if the accountType is of the type is **user**\n\n### custom policy to valid another account type?\n\nIn real life scenerio, a user model `accountType` might be an **admin**, **user**, **customer** or any other account type that fits your need. \nSimply copy the content of `UserIsUserPolicy` and paste in a new file eg `policies/userIsCustomerPolicy.js`. Then change the value of `ACCOUNT_TYPE` to match your need. Eg `ACCOUNT_TYPE = \"customer\"` \n\n### Apply policy\n\ngo to `config/policies.js` and apply the policy you need to the secure your routes. \nVisit sails doc [here](http://sailsjs.org/documentation/concepts/policies#?to-apply-a-policy-to-a-specific-controller-action) to learn more\n\n```javascript\n//example of how your file might look like\nmodule.exports.policies = {\n    '*': 'UserIsUserPolicy', //Secure all routes with UserIsUserPolicy\n    'JwtController': {\n        '*': true// Make this open to allow for signup and authentication\n    },\n    'AdminController': {\n        '*': 'UserIsAdminPolicy' //secure this route with UserIsAdminPolicy\n    },\n    'ProfileController': {\n        'destroy': 'UserIsAdminPolicy' //only admin can delete a profile, secured with UserIsAdminPolicy\n    }\n} \n```\n\n\n## Jwt routes (sign up / sign in)\n\n### signup\n\ndepening on the value of `authType` in `config/jsonWebToken.js` that you created, whose value could be `email`, or `username`.\n\nif `email`, simply send post request here `POST /jwt/signup` containing the following parameters.\n\n```javascript\n{\n    email: '',\n    password: '',//minimum length 4\n    accountType: '' //if absent, defaults to *user*\n}\n```\nif `username`, simply send post request here `POST /jwt/signup` containing the following parameters.\n\n```javascript\n{\n    username: '',\n    password: '',//minimum length 4\n    accountType: '' //if absent, defaults to *user*\n}\n```\n\nreturns object if successful. **NOTE** (`email` or `username`) would be part of the object returned depending on your `authType`\n\n\n```javascript\n{\n    user: {id: '', email: '', username: '', accountType: '', token: '', active: true},//contains user object\n    token: ''//deprecated, would be removed soon\n}\n```\n\n### signin\n\nsimply send post request here `POST /jwt/auth` containing the following parameters\n\nif `email`\n\n```javascript\n{\n    email: '',\n    password: '',//minimum length 4\n}\n```\n\nif `username`\n\n```javascript\n{\n    username: '',\n    password: '',//minimum length 4\n}\n```\n\nreturns object if successful. **NOTE**  (`email` or `username`) would be part of the object returned depending on your `authType`\n\n```javascript\n{\n    user: {id: '', email: '', username: '', accountType: '', token: '', active: true},//contains user object\n    token: ''//deprecated, would be removed soon\n}\n```\n\n\n## Accessing a secure route\n\nWhen acessing a route secured by policy, simple add token in Authorization header or through the route. See sample below where *token* is\n`QWxhZGRpbjpPcGVuU2VzYW1l`\n\n```html\nAuthorization: Bearer QWxhZGRpbjpPcGVuU2VzYW1l\n```\n\nor as parameter `token` in the request as shown below\n\n```html\nhttp://example.com?token=QWxhZGRpbjpPcGVuU2VzYW1l\n```\n\n\n## Using the JwtService\n\n**JwtService.issueToken(payload, user)** - This returns a promise containing a token for the user passed to it. `payload` is the content to be passed into the token and `user` is the model object of the user you want to generate a token for\n\n**JwtService.verifyToken(token)** - This returns a promise containing a decoded token if its still valid. `token` is the token you want to verify\n\n**JwtService.createUser(body)** - This returns a promise containing the new user object created. `body` same as object sent during [Signup](#signup) above\n\n**JwtService.getPasswordResetToken(email)** - This returns a promise containing a token that can be used for resetting the password for the `email` passed to the function\n\n**JwtService.resetPassword(newpassword, token)** - This returns a promise containing a message when the password is successfully changed. `newpassword` is the new password for the account while `token` is the token generated for the email, see `JwtService.getPasswordResetToken(email)` to get a token.\n\n## Changelog\n\nSee the different releases [here](https://github.com/Robophil/sails-hook-jsonwebtoken/releases)\n\n## Liscence\n\nMIT License\n","funding_links":[],"categories":["Hooks"],"sub_categories":["Free"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobophil%2Fsails-hook-jsonwebtoken","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobophil%2Fsails-hook-jsonwebtoken","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobophil%2Fsails-hook-jsonwebtoken/lists"}