{"id":26674569,"url":"https://github.com/dineshkumar-mb/mern-urlbackend","last_synced_at":"2025-03-26T02:20:15.402Z","repository":{"id":247927115,"uuid":"826818702","full_name":"dineshkumar-mb/mern-urlBackend","owner":"dineshkumar-mb","description":"url shortner with authentication ","archived":false,"fork":false,"pushed_at":"2024-07-11T11:41:21.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-07-12T11:28:38.664Z","etag":null,"topics":["authentication","jwt-token","mern"],"latest_commit_sha":null,"homepage":"https://mern-urlbackend.onrender.com","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/dineshkumar-mb.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}},"created_at":"2024-07-10T12:45:11.000Z","updated_at":"2024-07-11T11:41:25.000Z","dependencies_parsed_at":"2024-07-11T11:22:20.805Z","dependency_job_id":"c4284d77-7627-4c51-bea1-63a9877318cf","html_url":"https://github.com/dineshkumar-mb/mern-urlBackend","commit_stats":null,"previous_names":["dineshkumar-mb/mern-urlbackend"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dineshkumar-mb%2Fmern-urlBackend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dineshkumar-mb%2Fmern-urlBackend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dineshkumar-mb%2Fmern-urlBackend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dineshkumar-mb%2Fmern-urlBackend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dineshkumar-mb","download_url":"https://codeload.github.com/dineshkumar-mb/mern-urlBackend/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245573889,"owners_count":20637676,"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","jwt-token","mern"],"created_at":"2025-03-26T02:20:14.769Z","updated_at":"2025-03-26T02:20:15.383Z","avatar_url":"https://github.com/dineshkumar-mb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\nimplementing JWT authentication in a Node.js + Express.js API:\n\nInstall Dependencies: First, make sure you have Node.js and npm installed. Then, create a new Node.js project and install the necessary packages:\nnpm install express jsonwebtoken\n\nCreate an Authentication Middleware: You’ll need a middleware function to verify JWT tokens. Here’s a simple example:\nJavaScript\n\nconst jwt = require('jsonwebtoken');\n\nfunction authenticateToken(req, res, next) {\n    const authHeader = req.headers['authorization'];\n    const token = authHeader \u0026\u0026 authHeader.split(' ')[1]; // Extract the token from the Authorization header\n\n    if (token == null) {\n        return res.sendStatus(401); // Unauthorized\n    }\n\n    jwt.verify(token, 'your-secret-key', (err, user) =\u003e {\n        if (err) {\n            return res.sendStatus(403); // Forbidden\n        }\n        req.user = user; // Attach the user object to the request\n        next();\n    });\n}\nGenerate Tokens on Login: When a user logs in, generate a JWT token and send it back to the client:\nJavaScript\n\nconst token = jwt.sign({ username: 'user123' }, 'your-secret-key', { expiresIn: '1h' });\nres.json({ token });\n\nProtect Routes: Apply the authenticateToken middleware to routes that require authentication:\nJavaScript\n\napp.get('/protected-route', authenticateToken, (req, res) =\u003e {\n    // Access the user object from req.user\n    res.json({ message: 'Welcome to the protected route!' });\n},\nClient-Side Usage: In your client application (e.g., React, Angular, or Vue), store the token (usually in local storage) and include it in the Authorization header for authenticated requests.\nRemember to replace 'your-secret-key' with a strong, unique secret key for signing and verifying tokens. Also, consider using environment variables for sensitive information.\n\n\n\n## Deployment\n\n- Frontend: Deployed on [Netlify](https://chipper-marigold-f7720c.netlify.app)\n- Backend: Deployed on [Render](https://mern-urlbackend.onrender.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdineshkumar-mb%2Fmern-urlbackend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdineshkumar-mb%2Fmern-urlbackend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdineshkumar-mb%2Fmern-urlbackend/lists"}