{"id":21126938,"url":"https://github.com/ctrly4sh/node-auth-jwt","last_synced_at":"2026-04-16T04:31:03.391Z","repository":{"id":223917305,"uuid":"760068275","full_name":"ctrly4sh/node-auth-JWT","owner":"ctrly4sh","description":"Auth in node js + TypeScript using JWT","archived":false,"fork":false,"pushed_at":"2024-11-18T10:48:19.000Z","size":1988,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-04-08T20:15:33.085Z","etag":null,"topics":["backend","express","learning","nodejs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/ctrly4sh.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,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-02-19T18:14:17.000Z","updated_at":"2024-11-18T10:48:22.000Z","dependencies_parsed_at":"2025-04-24T18:38:15.326Z","dependency_job_id":"a6a96353-471e-49a0-84f0-145e9eccf426","html_url":"https://github.com/ctrly4sh/node-auth-JWT","commit_stats":null,"previous_names":["ctrly4sh/backend-development","ctrly4sh/json-filter-and-search","ctrly4sh/node-auth-jwt","yshtwr/node-auth-jwt"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ctrly4sh/node-auth-JWT","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctrly4sh%2Fnode-auth-JWT","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctrly4sh%2Fnode-auth-JWT/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctrly4sh%2Fnode-auth-JWT/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctrly4sh%2Fnode-auth-JWT/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ctrly4sh","download_url":"https://codeload.github.com/ctrly4sh/node-auth-JWT/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctrly4sh%2Fnode-auth-JWT/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31871392,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-15T15:24:51.572Z","status":"online","status_checked_at":"2026-04-16T02:00:06.042Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["backend","express","learning","nodejs"],"created_at":"2024-11-20T04:46:02.242Z","updated_at":"2026-04-16T04:31:03.373Z","avatar_url":"https://github.com/ctrly4sh.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Internship task : \n\n Workflow Overview \n\n1.  User Types : You’re dealing with three types of users:\n   -  Buyer : Session expires in  2 days .\n   -  Seller : Session expires in  5 days .\n   -  Admin : Session expires in  7 days .\n\n2.  Basic Components :\n   -  JWT : Used for generating, signing, and verifying tokens.\n   -  Middleware : Ensures only authenticated users access certain routes.\n   -  Role Management : Some routes may be restricted based on user roles.\n\n \n\n Detailed Step-by-Step Workflow \n\n# 1. Registration (`POST /register`) \n\nThis route is for user signup.  \nHere’s how it works:\n\n-  Request : \n  - The user sends their  username ,  password , and  role  (e.g., `Buyer`, `Seller`, or `Admin`) via Postman.\n  \n-  Backend Logic :\n  - Hash the password using  bcrypt  for security.\n  - Save the user data in your database (like MongoDB, PostgreSQL, etc.), including their role.\n\n-  Response :\n  - Return a success message confirming registration.\n\n \n\n# 2. Login (`POST /login`) \n\nThis route is for authentication and issuing the JWT.  \nHere’s the process:\n\n-  Request :\n  - User sends their  username  and  password  via Postman.\n\n-  Backend Logic :\n  1. Validate credentials:\n     - Check the username and password against the database.\n  2. Determine role:\n     - Fetch the user's role from the database.\n  3. Generate JWT:\n     - Create a JWT with the following payload:\n        json\n       {\n         \"id\": \"user_id\",\n         \"role\": \"Buyer\",\n         \"iat\": \u003ctimestamp\u003e,\n         \"exp\": \u003ctimestamp_based_on_role\u003e\n       }\n        \n     - For the `exp` (expiration) field:\n       -  Buyer : Current time + 2 days.\n       -  Seller : Current time + 5 days.\n       -  Admin : Current time + 7 days.\n     - Sign the token with a  secret key  (e.g., `process.env.JWT_SECRET`).\n\n-  Response :\n  - Return the JWT to the user in the response body (or as an HTTP-only cookie for better security).\n\n \n\n# 3. Protected Routes (e.g., `GET /protected-route`) \n\nThis route demonstrates how session management works.  \n Example Route : Only authenticated users can access it.\n\n-  Request :\n  - The user includes their JWT in the `Authorization` header:\n     \n    Authorization: Bearer \u003cJWT_TOKEN\u003e\n     \n\n-  Backend Logic  (via Middleware):\n  1.  Extract and Validate the Token :\n     - Extract the token from the `Authorization` header.\n     - Verify the token using your secret key.\n     - Decode the token to get the user information (e.g., `role`, `id`).\n  2.  Check Expiry :\n     - If the token is expired, reject the request with a `401 Unauthorized` error.\n  3.  Allow Access :\n     - If the token is valid, let the request proceed to the route handler.\n\n-  Response :\n  - If valid, return the requested resource.\n  - If invalid, return an error message.\n\n \n\n# 4. Role-Based Access Control (RBAC) \n\nSome routes should only be accessible to specific roles.\n\n-  Example : \n  - A `GET /admin-dashboard` route should only allow `Admin` users.\n  \n-  Backend Logic  (via Middleware):\n  1. Check the token’s role (`role` field).\n  2. If the role doesn’t match the required one (`Admin` in this case), reject the request with a `403 Forbidden` error.\n  3. Allow the request to proceed if the role matches.\n\n \n\n# 5. Token Expiration \n\n- Each token has an expiration time (`exp` field in JWT).  \n- When the token expires:\n  - Middleware automatically rejects requests with expired tokens (this is handled by most JWT libraries during token verification).\n  - The user will need to log in again to get a new token.\n\n \n\n# 6. Logout (`POST /logout`) \n\nTechnically, with JWT, logout is  handled on the client side  by deleting the token.  \nBut if you want to implement server-side logout:\n\n- Store a blacklist of tokens (optional).  \n- When a user logs out, add their token to the blacklist.\n- Middleware checks every incoming token against the blacklist.\n\n \n\n API Endpoints Summary \n\n|  Route               |  Method    |  Description                                           |\n----------------------------------------------------------------------------------------------\n|                      |            |                                                        |\n| `/register`          | `POST`     | Register a new user (Buyer, Seller, or Admin).         |\n| `/login`             | `POST`     | Log in the user and return a JWT.                      |\n| `/protected-route`   | `GET`      | A test route; requires a valid JWT to access.          |\n| `/admin-dashboard`   | `GET`      | Restricted to Admin users only.                        |\n| `/logout` (optional) | `POST`     | Logout and blacklist the current JWT (if implemented). |\n\n \n\n Testing with Postman \n\nHere’s how you can test each endpoint:\n\n1.  Register :\n   - URL: `http://localhost:3000/register`\n   - Method: `POST`\n   - Body (JSON):\n      json\n     {\n       \"username\": \"john_doe\",\n       \"password\": \"password123\",\n       \"role\": \"Buyer\"\n     }\n      \n\n2.  Login :\n   - URL: `http://localhost:3000/login`\n   - Method: `POST`\n   - Body (JSON):\n      json\n     {\n       \"username\": \"john_doe\",\n       \"password\": \"password123\"\n     }\n      \n   - Response: Copy the token from the response.\n\n3.  Access Protected Route :\n   - URL: `http://localhost:3000/protected-route`\n   - Method: `GET`\n   - Headers:\n      \n     Authorization: Bearer \u003cJWT_TOKEN\u003e\n      \n\n4.  Access Admin-Only Route :\n   - Use an Admin token and test access to an admin-only route.\n\n5.  Logout (Optional) :\n   - URL: `http://localhost:3000/logout`\n   - Method: `POST`\n   - Body: (No specific body needed).\n\n \n\n Key Notes \n-  Testing Without Frontend : Postman is sufficient for testing all the backend logic.\n-  Session Management :\n  - Managed via token expiration (`exp` field).\n  - Different roles = Different token lifespans.\n-  Middleware :\n  - For authentication: Verifies the token.\n  - For role-based access: Ensures only specific roles access restricted routes.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctrly4sh%2Fnode-auth-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fctrly4sh%2Fnode-auth-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctrly4sh%2Fnode-auth-jwt/lists"}