Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update documentation #387

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,19 +509,7 @@ Configuration passed to `firebase-admin`'s [`initializeApp`](https://firebase.go

The `firebaseAdminInitConfig.credential.privateKey` cannot be defined on the client side and should live in a secret environment variable.

> Note: if using environment variables in Vercel, add the private key _with double quotes_ via the CLI:
>
> `vercel secrets add firebase-private-key '"my-key-here"'`
>
> Then, use `JSON.parse` in the `firebaseAdminInitConfig.credential.privateKey` property:
>
> ```
> privateKey: process.env.FIREBASE_PRIVATE_KEY
> ? JSON.parse(process.env.FIREBASE_PRIVATE_KEY)
> : undefined
> ```
>
> See [this Vercel issue](https://github.com/vercel/vercel/issues/749#issuecomment-707515089) for more information.
> ℹ️ Using Vercel? See [adding a private key to Vercel](#adding-a-private-key-to-Vercel) for guidance.

#### useFirebaseAdminDefaultCredential

Expand Down Expand Up @@ -651,11 +639,45 @@ A method that calls Firebase's [`signOut`](https://firebase.google.com/docs/refe

## Examples

- [Adding a private key to Vercel](#adding-a-private-key-to-Vercel)
- [Using the Firebase Apps](#using-the-firebase-apps)
- [TypeScript](#typescript)
- [Dynamic Redirects](#dynamic-redirects)
- [Testing and Mocking with Jest](#testing-and-mocking-with-jest)

### Adding a private key to Vercel

There are various ways to add your Firebase private key as an environment variable to Vercel.

__Vercel console__

In the [Vercel console](https://vercel.com/docs/concepts/projects/environment-variables), add the private key in double quotes (screenshot [here](https://github.com/gladly-team/next-firebase-auth/issues/212)).

Then, use the private key in your `next-firebase-auth` config, in the `firebaseAdminInitConfig.credential.privateKey` property:


```javascript
privateKey: process.env.FIREBASE_PRIVATE_KEY
```

__Vercel CLI__

Via the Vercel CLI, add the private key _with double quotes_:

`vercel secrets add firebase-private-key '"my-key-here"'`

Then, use `JSON.parse` in the `firebaseAdminInitConfig.credential.privateKey` property:

```javascript
privateKey: process.env.FIREBASE_PRIVATE_KEY
? JSON.parse(process.env.FIREBASE_PRIVATE_KEY)
: undefined
```

__Alternative formatting__

Others have taken different approaches to deal with escaped newline characters in the private key; for example, by [using string replacement](https://stackoverflow.com/a/50376092). This discussion includes other approaches: [discussion #95](https://github.com/gladly-team/next-firebase-auth/discussions/95)

### Using the Firebase Apps

You may want to access the Firebase admin module or Firebase JS SDK.
Expand Down Expand Up @@ -1019,6 +1041,10 @@ You can try setting up your credentials in [the example app](https://github.com/

In local development, try clearing data/cookies for `localhost` in case you previously signed in with another Firebase account and still have auth cookies signed by another private key.

#### I get the error, "Failed to parse private key: Error: Invalid PEM formatted message."

See [adding a private key to Vercel](#adding-a-private-key-to-Vercel) and [this discussion](https://github.com/gladly-team/next-firebase-auth/discussions/95) on private key formatting.

## Limitations & Feedback

We expect some apps will need some features that are not currently available:
Expand Down
4 changes: 4 additions & 0 deletions example/pages/ssr-auth-required.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export const getServerSideProps = withAuthUserTokenSSR({
// `getServerSideProps`, including redirects.
// https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering
const token = await AuthUser.getIdToken()

// Note: you shouldn't typically fetch your own API routes from within
// `getServerSideProps`. This is for example purposes only.
// https://github.com/gladly-team/next-firebase-auth/issues/264
const endpoint = getAbsoluteURL('/api/example', req)
const response = await fetch(endpoint, {
method: 'GET',
Expand Down