{"id":20186110,"url":"https://github.com/leolanese/nodejs_jwt_authentification_api","last_synced_at":"2026-04-14T04:02:25.763Z","repository":{"id":175550538,"uuid":"654066240","full_name":"leolanese/NodeJS_JWT_Authentification_API","owner":"leolanese","description":"Building JWT Authentication API With NodeJS and MondogDB using Cluster","archived":false,"fork":false,"pushed_at":"2024-02-09T12:34:21.000Z","size":324,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-13T03:33:22.149Z","etag":null,"topics":["javascript","jwt","mongodb","mongoose","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leolanese.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-06-15T10:11:15.000Z","updated_at":"2025-08-14T09:42:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"efc86bff-56da-4e44-a398-099d0bb3f4bc","html_url":"https://github.com/leolanese/NodeJS_JWT_Authentification_API","commit_stats":null,"previous_names":["leolanese/nodejs_jwt_authentification_api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/leolanese/NodeJS_JWT_Authentification_API","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leolanese%2FNodeJS_JWT_Authentification_API","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leolanese%2FNodeJS_JWT_Authentification_API/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leolanese%2FNodeJS_JWT_Authentification_API/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leolanese%2FNodeJS_JWT_Authentification_API/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leolanese","download_url":"https://codeload.github.com/leolanese/NodeJS_JWT_Authentification_API/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leolanese%2FNodeJS_JWT_Authentification_API/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31781292,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-14T02:24:21.117Z","status":"ssl_error","status_checked_at":"2026-04-14T02:24:20.627Z","response_time":153,"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":["javascript","jwt","mongodb","mongoose","nodejs"],"created_at":"2024-11-14T03:16:01.652Z","updated_at":"2026-04-14T04:02:25.747Z","avatar_url":"https://github.com/leolanese.png","language":"JavaScript","readme":"# NodeJS JWT Authentication API\n\n\u003e Building JWT Authentication API With NodeJS and MondogDB using Cluster\n\nThis codebase is essentially creating a Node.js-based REST API for user registration and login. The key concepts involved are JWT (JSON Web Tokens) and MongoDB, used for data persistence. \n\n## A brief overview:\n\n- User Registration: The user provides their name, email, and password to register for an account. When the request is sent to the /user endpoint, the application first checks if a user with the provided email already exists in the MongoDB database. If not, the password is hashed for security reasons, and then the new user's data is stored in the database. Finally, a JWT is generated using the user's unique identifier (id), which can be used for authenticating future requests.\n\n- User Login: The user provides their email and password to log in. When the request is sent to the /login endpoint, the application first finds the user in the MongoDB database by email. Then, it compares the provided password with the hashed password stored in the database using bcrypt. If they match, it means the password is correct. Finally, a JWT is generated for the user, similar to the registration process.\n\n- JWT: JSON Web Tokens (JWTs) are a standard for securely transmitting information between parties as a JSON object. They are used here to handle user sessions. After a user logs in or registers, they receive a JWT. For subsequent requests that need authentication, the user sends this token in the headers. The server can then verify this token and allow the user to access protected routes.\n\n- MongoDB Cluster: MongoDB, a NoSQL document database, is being used to persist user data. In this codebase, the MongoDB Atlas Cluster is being used, which is a fully-managed cloud database provided by MongoDB. It's scalable and allows you to distribute your data across multiple servers for high availability and performance.\n\nThis API is built following the MVC (Model-View-Controller) pattern and uses the Express.js framework for routing and middleware. It also uses the mongoose package to interact with MongoDB and enforce the data schema, and bcryptjs for hashing passwords. The jsonwebtoken package is used for creating JWTs and express-async-handler for handling asynchronous route handlers.\n\n---\n\n## Let's start the project\n\n```js\n// we need start creating the `package.json`\nnpm init\n```\n\n```js\nnpm i express bcryptjs express-async-handler jsonwebtoken mongoose morgan\n```\n\n```js\nnpm i -D nodemon\n```\n\n---\n\n## Setup environment\n\nLet's create a new file `.env` and `.gitignore`\n\n```js\n// .env\nPORT = 5050\nMODE = development\n```\n\n```js\n// .gitignore\n.env\n**/node_modules\n```\n\n---\n\n## Update type in package.json\n\n\u003e \"type\": \"module\" in your package.json file, which instructs Node.js to treat .js files as ES Modules\n\u003e In brief: Replace `require` (CommonJS style) with `import` (ESM Style) from now on\n\n```js\n...\n  \"type\": \"module\",\n...\n```\n\n---\n\n## Connecting to the MongoDb server Cluster\n\nWe need Node.js server to rung successfully on port 5050 and it also connected to MongoDB successfully. Just to mention, for simplicity MVC pattern will be in place\n\n## Running server\n\n```js\nnpm run dev\n\n...\n[nodemon] starting `node server.js`\nServer running on: 5050\nMongoDB is Connected successfully: ac-okc09tz-shard-00-01.ooedapl.mongodb.net\n```\n\n![MongoDb Cluster](./shared/imgs/mongodb-atlas.jpg)\n\n---\n\n## Testing API Requests/Responses\n\n### Test POST (invalid)\n\n![PostMan API POST testing](./shared/imgs/invalid.jpg)\n\n### Test POST (valid register)\n\n![Postman API POST valid register](./shared/imgs/register.jpg)\n\n### Test POST (invalid register)\n\n![Postman API POST invalid register](./shared/imgs/register-invalid.jpg)\n\n### Test POST (login success after valid register)\n\n![Postman API POST valid login](./shared/imgs/login-success.jpg)\n\n### Test GET (login into private router successfully after register)\n\n\u003e TIP: We need to add Headers: `Key`: `Authorization` \u0026 `Value`: `\u003ctoken-value-here\u003e`\n\n![Postman API GET valid](./shared/imgs/account.jpg)\n\n---\n### :100: \u003ci\u003eThanks!\u003c/i\u003e\n#### Now, don't be an stranger. Let's stay in touch!\n\n##### :radio_button: linkedin: \u003ca href=\"https://www.linkedin.com/in/leolanese/\" target=\"_blank\"\u003e@LeoLanese\u003c/a\u003e\n##### :radio_button: Twitter: \u003ca href=\"https://twitter.com/LeoLanese\" target=\"_blank\"\u003e@LeoLanese\u003c/a\u003e\n##### :radio_button: Portfolio: \u003ca href=\"https://www.leolanese.com\" target=\"_blank\"\u003ewww.leolanese.com\u003c/a\u003e\n##### :radio_button: DEV.to: \u003ca href=\"https://www.dev.to/leolanese\" target=\"_blank\"\u003edev.to/leolanese\u003c/a\u003e\n##### :radio_button: Blog: \u003ca href=\"https://www.leolanese.com/blog\" target=\"_blank\"\u003eleolanese.com/blog\u003c/a\u003e\n##### :radio_button: Questions / Suggestion / Recommendation: developer@leolanese.com\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleolanese%2Fnodejs_jwt_authentification_api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleolanese%2Fnodejs_jwt_authentification_api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleolanese%2Fnodejs_jwt_authentification_api/lists"}