{"id":18762747,"url":"https://github.com/imabhinavdev/easy-jwt-auth","last_synced_at":"2026-02-10T19:02:38.392Z","repository":{"id":249485888,"uuid":"831668207","full_name":"imabhinavdev/easy-jwt-auth","owner":"imabhinavdev","description":"A lightweight Node.js library that simplifies JWT authentication. Provides methods for password hashing, token generation, route protection, and token refresh. Ideal for integrating JWT with Mongoose schemas.","archived":false,"fork":false,"pushed_at":"2024-08-02T03:50:51.000Z","size":232,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-03T19:59:55.087Z","etag":null,"topics":["authentication","authentication-library","authentication-middleware","javascript","jsonwebtoken","jwt","jwt-authentication","library","npm","npm-package"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/easy-jwt-auth","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/imabhinavdev.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":"2024-07-21T09:10:42.000Z","updated_at":"2024-08-02T03:50:54.000Z","dependencies_parsed_at":"2024-07-21T09:25:14.354Z","dependency_job_id":"8f2fddae-e2ef-497e-9a8a-d465e8f18e33","html_url":"https://github.com/imabhinavdev/easy-jwt-auth","commit_stats":null,"previous_names":["imabhinavdev/easy-jwt-auth"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/imabhinavdev/easy-jwt-auth","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imabhinavdev%2Feasy-jwt-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imabhinavdev%2Feasy-jwt-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imabhinavdev%2Feasy-jwt-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imabhinavdev%2Feasy-jwt-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imabhinavdev","download_url":"https://codeload.github.com/imabhinavdev/easy-jwt-auth/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imabhinavdev%2Feasy-jwt-auth/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29313010,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-10T17:48:59.043Z","status":"ssl_error","status_checked_at":"2026-02-10T17:45:37.240Z","response_time":65,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: 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":["authentication","authentication-library","authentication-middleware","javascript","jsonwebtoken","jwt","jwt-authentication","library","npm","npm-package"],"created_at":"2024-11-07T18:23:07.585Z","updated_at":"2026-02-10T19:02:38.346Z","avatar_url":"https://github.com/imabhinavdev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Here's the updated README file with the added information about the available methods after using `addJWTMethodsToSchema`:\n\n---\n\n# Easy JWT Auth\n\nEasy JWT Auth is a lightweight library designed to simplify the implementation of JWT authentication in your Node.js applications. This library provides methods to integrate JWT authentication seamlessly with your schema, handle user authentication, and manage token refresh and sign-out processes.\n\n## Installation\n\nYou can install Easy JWT Auth using npm:\n\n```bash\nnpm install easy-jwt-auth\n```\n\n## Usage\n\n### Importing the Library\n\nTo use Easy JWT Auth, import the required functions into your application:\n\n```javascript\nimport { addJWTMethodsToSchema, authMiddleware, signout, refreshToken } from 'easy-jwt-auth';\n```\n\n### Functions\n\n#### `addJWTMethodsToSchema(schema, secret, uniqueKey, password)`\n\nThis function adds JWT methods to your schema for user authentication. It handles password hashing, token generation, and password comparison.\n\n**Parameters:**\n- `schema`: The Mongoose schema to which the JWT methods will be added.\n- `secret`: The secret key used for signing JWTs.\n- `uniqueKey` (optional): The unique key field in the schema (default is 'email').\n- `password` (optional): The password field in the schema (default is 'password').\n\n**Usage Example:**\n\n```javascript\nimport mongoose from 'mongoose';\nimport { addJWTMethodsToSchema } from 'easy-jwt-auth';\n\nconst userSchema = new mongoose.Schema({\n  // Define your schema fields here\n});\n\naddJWTMethodsToSchema(userSchema, 'your-secret-key');\n\nconst User = mongoose.model('User', userSchema);\n```\n\n**Methods Added to Schema:**\n\n| Method              | Description                                                         | Parameters                                           |\n|---------------------|---------------------------------------------------------------------|------------------------------------------------------|\n| `matchPassword`     | Compares the entered password with the hashed password stored in the database. | `enteredPassword`: The password to compare.         |\n| `generateToken`     | Generates a JWT for the user.                                        | `secret`: The secret key used for signing the token.\u003cbr\u003e`expiresIn` (optional): The expiration time of the token (default is '1h'). |\n| `generateRefreshToken` | Generates a refresh token for the user.                              | `secret`: The secret key used for signing the refresh token.\u003cbr\u003e`expiresIn` (optional): The expiration time of the refresh token (default is '7d'). |\n\n#### `authMiddleware({ secret, except, algorithms })`\n\nThis middleware function checks for a valid JWT in the request headers and attaches the decoded user data to the request object.\n\n**Parameters:**\n- `secret`: The secret key used for verifying JWTs.\n- `except` (optional): An array of routes to exclude from authentication (default is an empty array).\n- `algorithms` (optional): An array of algorithms to use for verifying the JWT (default is [\"HS256\"]).\n\n**Usage Example:**\n\n```javascript\nimport express from 'express';\nimport { authMiddleware } from 'easy-jwt-auth';\n\nconst app = express();\n\napp.use(authMiddleware({ secret: 'your-secret-key', except: ['/login', '/register'] }));\n\napp.get('/protected-route', (req, res) =\u003e {\n  res.json({ message: 'You have access to this protected route', user: req.user });\n});\n```\n\n#### `refreshToken(secret)`\n\nThis function handles refreshing the access token using a refresh token.\n\n**Parameters:**\n- `secret`: The secret key used for verifying the refresh token.\n\n**Usage Example:**\n\n```javascript\nimport express from 'express';\nimport { refreshToken } from 'easy-jwt-auth';\n\nconst app = express();\n\napp.post('/refresh-token', refreshToken('your-secret-key'));\n```\n\n#### `signout(req, res)`\n\nThis function handles signing out the user by clearing the access and refresh tokens.\n\n**Usage Example:**\n\n```javascript\nimport express from 'express';\nimport { signout } from 'easy-jwt-auth';\n\nconst app = express();\n\napp.post('/signout', signout);\n```\n\n## Example\n\nHere is a complete example of how to use Easy JWT Auth in an Express application:\n\n```javascript\nimport express from 'express';\nimport mongoose from 'mongoose';\nimport { addJWTMethodsToSchema, authMiddleware, signout, refreshToken } from 'easy-jwt-auth';\n\nconst app = express();\napp.use(express.json());\n\napp.use(authMiddleware({ secret: 'your-secret-key', except: ['/login', '/register'] }));\n\nconst userSchema = new mongoose.Schema({\n  // Define your schema fields here\n});\n\naddJWTMethodsToSchema(userSchema, 'your-secret-key');\nconst User = mongoose.model('User', userSchema);\n\napp.post('/register', async (req, res) =\u003e {\n  const user = new User(req.body);\n  await user.save();\n  res.status(201).json(user);\n});\n\napp.post('/login', async (req, res) =\u003e {\n  const { email, password } = req.body;\n  const user = await User.findOne({ email }).select('+password');\n  if (!user || !(await user.matchPassword(password))) {\n    return res.status(401).json({ message: 'Invalid email or password' });\n  }\n  const token = user.generateToken('your-secret-key');\n  const refreshToken = user.generateRefreshToken('your-secret-key');\n  res.json({ token, refreshToken });\n});\n\napp.post('/refresh-token', refreshToken('your-secret-key'));\n\napp.post('/signout', signout);\n\napp.listen(3000, () =\u003e {\n  console.log('Server is running on port 3000');\n});\n```\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n---\n\nFeel free to contribute to this project by submitting issues or pull requests. Your feedback is highly appreciated!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimabhinavdev%2Feasy-jwt-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimabhinavdev%2Feasy-jwt-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimabhinavdev%2Feasy-jwt-auth/lists"}