Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danecando/with-firebase-user
A higher order function that decodes a Firebase Auth JWT and decorates the NextJS api request object with a Firebase user
https://github.com/danecando/with-firebase-user
authentication firebase firebase-auth jwt nextjs
Last synced: 21 days ago
JSON representation
A higher order function that decodes a Firebase Auth JWT and decorates the NextJS api request object with a Firebase user
- Host: GitHub
- URL: https://github.com/danecando/with-firebase-user
- Owner: danecando
- Created: 2021-10-18T01:35:12.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-22T12:23:15.000Z (about 2 years ago)
- Last Synced: 2024-04-25T09:42:16.093Z (7 months ago)
- Topics: authentication, firebase, firebase-auth, jwt, nextjs
- Language: TypeScript
- Homepage:
- Size: 89.8 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# with-firebase-user
A higher order function that decodes a Firebase Auth JWT and decorates the NextJS API request object with a Firebase user.
## Introduction
I wanted to use Firebase Auth to authenticate and secure routes in my NextJS API.
**The Problem**
- Unable to add middleware to express middleware chain for NextJS API routes.
- Don't want to add a large library for a lightweight operation to be run on a serverless function.**My Solution**
- 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.
- Write a higher order function to decorate the NextJS request object with authenticated Firebase user data.## Installation
```bash
npm i --save with-firebase-user
```or
```bash
yarn add with-firebase-user
```## Usage
Pass your NextJS handler to `withFirebaseUser` and it will add the authenticated user to the request object `user` key.
```js
import { withFirebaseUser } from 'with-firebase-user';const handler = async (req, res) => {
res.send(req.user); // FirebaseUser
};export default withFirebaseUser(handler);
```Make sure to add your JWT to the request headers on the client.
```javascript
fetch('/api/users', {
headers: {
Authorization: 'Bearer ',
},
});
```### Decoded user data
```ts
interface FirebaseUser {
name?: string;
user_id: string;
email?: string;
email_verified?: boolean;
}
```## API
```typescript
const withFirebaseUser: (
handler: (
req: NextApiRequestWithFirebaseUser,
res: NextApiResponse
) => Promise,
options?: WithFirebaseUserOptions | undefined
) => (req: NextApiRequest, res: NextApiResponse) => Promise;
```### WithFirebaseUserOptions (passed as a second argument to withFirebaseUser function)
```typescript
interface WithFirebaseUserOptions {
clientCertUrl?: string; // defaults to url provided in Firebase auth docs
projectId?: string; // verifies the audience and issuer of the JWT when provided
}
```