{"id":15992391,"url":"https://github.com/danecando/with-firebase-user","last_synced_at":"2025-03-17T23:30:36.246Z","repository":{"id":57398411,"uuid":"418304312","full_name":"danecando/with-firebase-user","owner":"danecando","description":"A higher order function that decodes a Firebase Auth JWT and decorates the NextJS api request object with a Firebase user","archived":false,"fork":false,"pushed_at":"2022-08-22T12:23:15.000Z","size":92,"stargazers_count":2,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-25T09:42:16.093Z","etag":null,"topics":["authentication","firebase","firebase-auth","jwt","nextjs"],"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/danecando.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}},"created_at":"2021-10-18T01:35:12.000Z","updated_at":"2022-08-20T18:15:25.000Z","dependencies_parsed_at":"2022-09-04T03:23:01.205Z","dependency_job_id":null,"html_url":"https://github.com/danecando/with-firebase-user","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danecando%2Fwith-firebase-user","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danecando%2Fwith-firebase-user/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danecando%2Fwith-firebase-user/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danecando%2Fwith-firebase-user/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danecando","download_url":"https://codeload.github.com/danecando/with-firebase-user/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221702875,"owners_count":16866446,"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","firebase","firebase-auth","jwt","nextjs"],"created_at":"2024-10-08T06:21:35.717Z","updated_at":"2024-10-27T16:12:20.167Z","avatar_url":"https://github.com/danecando.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# with-firebase-user\n\nA higher order function that decodes a Firebase Auth JWT and decorates the NextJS API request object with a Firebase user.\n\n## Introduction\n\nI wanted to use Firebase Auth to authenticate and secure routes in my NextJS API.\n\n**The Problem**\n\n- Unable to add middleware to express middleware chain for NextJS API routes.\n- Don't want to add a large library for a lightweight operation to be run on a serverless function.\n\n**My Solution**\n\n- Follow the [Verify ID Tokens using a third party JWT library](https://firebase.google.com/docs/auth/admin/verify-id-tokens#verify_id_tokens_using_a_third-party_jwt_library) instructions from the firebase auth docs to securely verify and decode the JWT.\n- Write a higher order function to decorate the NextJS request object with authenticated Firebase user data.\n\n## Installation\n\n```bash\nnpm i --save with-firebase-user\n```\n\nor\n\n```bash\nyarn add with-firebase-user\n```\n\n## Usage\n\nPass your NextJS handler to `withFirebaseUser` and it will add the authenticated user to the request object `user` key.\n\n```js\nimport { withFirebaseUser } from 'with-firebase-user';\n\nconst handler = async (req, res) =\u003e {\n  res.send(req.user); // FirebaseUser\n};\n\nexport default withFirebaseUser(handler);\n```\n\nMake sure to add your JWT to the request headers on the client.\n\n```javascript\nfetch('/api/users', {\n  headers: {\n    Authorization: 'Bearer \u003cJWT\u003e',\n  },\n});\n```\n\n### Decoded user data\n\n```ts\ninterface FirebaseUser {\n  name?: string;\n  user_id: string;\n  email?: string;\n  email_verified?: boolean;\n}\n```\n\n## API\n\n```typescript\nconst withFirebaseUser: (\n  handler: (\n    req: NextApiRequestWithFirebaseUser,\n    res: NextApiResponse\n  ) =\u003e Promise\u003cvoid\u003e,\n  options?: WithFirebaseUserOptions | undefined\n) =\u003e (req: NextApiRequest, res: NextApiResponse) =\u003e Promise\u003cvoid\u003e;\n```\n\n### WithFirebaseUserOptions (passed as a second argument to withFirebaseUser function)\n\n```typescript\ninterface WithFirebaseUserOptions {\n  clientCertUrl?: string; // defaults to url provided in Firebase auth docs\n  projectId?: string; // verifies the audience and issuer of the JWT when provided\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanecando%2Fwith-firebase-user","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanecando%2Fwith-firebase-user","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanecando%2Fwith-firebase-user/lists"}