Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
120 commits
Select commit Hold shift + click to select a range
104c8a6
Create Next config in example app
kmjennison May 14, 2021
20dc822
Upgrade example app dependencies
kmjennison May 14, 2021
b5087e8
0.13.1
kmjennison May 14, 2021
f3e477c
Update README.md
kmjennison May 14, 2021
149bf76
Merge branch 'main' into kevin/update-example
kmjennison May 14, 2021
4a1f81a
Update NFA
kmjennison May 14, 2021
9ed785f
Merge pull request #177 from gladly-team/kevin/update-example
kmjennison May 14, 2021
fe70427
Update README.md
kmjennison May 17, 2021
9b1c67f
Update README.md
kmjennison May 17, 2021
01ba745
Update README.md
kmjennison May 17, 2021
111570c
Update README.md
kmjennison May 17, 2021
6d4b884
Update README.md
kmjennison Jun 3, 2021
9cc9962
docs (README): Clearer explanation about cookies keys
Izhaki Jun 9, 2021
736b15c
A note about keys being secrets
Izhaki Jun 9, 2021
5630295
Merge pull request #198 from Izhaki/patch-1
kmjennison Jun 10, 2021
172c760
Update README.md
kmjennison Jun 10, 2021
207cfca
Update README.md
kmjennison Jun 10, 2021
24b8c96
Support Next 11
kmjennison Jun 27, 2021
d48a48b
Merge pull request #220 from gladly-team/kevin/next-11
kmjennison Jun 27, 2021
2012af6
0.13.2
kmjennison Jun 27, 2021
201ffc1
Upgrade example to Next 11
kmjennison Jun 27, 2021
c7553eb
Merge pull request #221 from gladly-team/kevin/update-example
kmjennison Jun 27, 2021
3a9844d
Update README.md
kmjennison Jun 27, 2021
3174d69
Merge pull request #222 from gladly-team/kevin/init-docs
kmjennison Jun 27, 2021
d0e7524
add static hoisting
Jul 25, 2021
f5a8bd4
WIP - add dynamic redirect to example
kmjennison Jul 29, 2021
4e9941b
Add a property to useFirebaseUser to know when cookies have (likely) …
kmjennison Jul 29, 2021
6e17834
Change property name
kmjennison Jul 29, 2021
64a5cc9
On the client side, redirect after the cookie request has completed
kmjennison Jul 29, 2021
8316986
Add link to issue
kmjennison Jul 29, 2021
40749c6
Fix render logic to wait for auth request
kmjennison Jul 29, 2021
b484f24
Change property name
kmjennison Jul 29, 2021
b1f725b
Add test
kmjennison Jul 29, 2021
8574bdf
Update debug logging
kmjennison Jul 30, 2021
a206bcf
Add TODOs
kmjennison Jul 30, 2021
3ba1caa
Reset state for repeated auth requests
kmjennison Jul 30, 2021
5b54c95
Don't modify useFirebaseUser state after unmount
kmjennison Jul 30, 2021
2405a54
Lint fixes
kmjennison Jul 30, 2021
ed7256e
Merge pull request #243 from gladly-team/kevin/fix-race-condition
kmjennison Jul 30, 2021
58ea8ae
Upgrade dependencies and lint
kmjennison Jul 30, 2021
b111442
Merge pull request #245 from gladly-team/kevin/upgrade-deps
kmjennison Jul 30, 2021
61b7209
Combine state updates in useFirebaseUser to reduce renders
kmjennison Jul 30, 2021
0047376
Merge pull request #246 from gladly-team/kevin/combine-state-updates
kmjennison Jul 30, 2021
c53f254
Add test
kmjennison Jul 30, 2021
67695b5
Redact config secrets when debug logging
kmjennison Jul 30, 2021
75f256a
Lint fix
kmjennison Jul 30, 2021
05ffb64
Merge branch 'main' into add_static_hoisting
kmjennison Jul 30, 2021
78a5dfc
Merge pull request #247 from gladly-team/kevin/debug-hide-config-secrets
kmjennison Jul 30, 2021
943de04
Update withAuthUser.js
kmjennison Jul 30, 2021
ee9d8a3
Update withAuthUser.js
kmjennison Jul 30, 2021
3d1d1f2
Upgrade example app dependencies
kmjennison Jul 30, 2021
57ec10f
Enable debugging for the example app by default
kmjennison Jul 30, 2021
f19ab16
Prevent state updates after unmount
kmjennison Jul 30, 2021
d447c1c
Use useMemo in withAuthUser to potentially avoid an unnecessary re-re…
kmjennison Aug 2, 2021
a3b8a87
Merge pull request #254 from gladly-team/kevin/add-use-memo
kmjennison Aug 2, 2021
6cdb8e4
Merge pull request #237 from uncvrd/add_static_hoisting
kmjennison Aug 2, 2021
850f6b4
Update lockfile
kmjennison Aug 2, 2021
b546bc9
Upgrade some dependencies
kmjennison Aug 2, 2021
a5628db
Merge branch 'main' into kevin/update-example
kmjennison Aug 2, 2021
a3d59c8
0.13.3-alpha.0
kmjennison Aug 2, 2021
f8952bb
Upgrade NFA
kmjennison Aug 2, 2021
a29a36c
Merge pull request #255 from gladly-team/kevin/update-example
kmjennison Aug 2, 2021
05931b5
Merge branch 'main' into kevin/dynamic-redirect-docs
kmjennison Aug 2, 2021
29b8aa5
Add eslint-config-next dev dependency to the example app (for local b…
kmjennison Aug 2, 2021
bea93f7
Merge pull request #256 from gladly-team/kevin/add-eslint-config
kmjennison Aug 2, 2021
2f4d43d
Add bundle analyzer to example app
kmjennison Aug 3, 2021
88f76fc
Merge pull request #258 from gladly-team/kevin/add-bundle-analyzer
kmjennison Aug 3, 2021
bde5a8d
Add hoist-non-react-statics to Node externals whitelist
kmjennison Aug 3, 2021
64946f5
Merge pull request #259 from gladly-team/kevin/add-reqd-external
kmjennison Aug 3, 2021
a5cfba1
0.13.3-alpha.1
kmjennison Aug 3, 2021
b03c470
Update example
kmjennison Aug 3, 2021
2499927
Merge pull request #260 from gladly-team/kevin/update-example
kmjennison Aug 3, 2021
fdda440
Merge branch 'main' into kevin/dynamic-redirect-docs
kmjennison Aug 3, 2021
567038d
Fix redirect URL example
kmjennison Aug 3, 2021
966f954
Remove redirect example
kmjennison Aug 3, 2021
b033eea
Update README.md
kmjennison Aug 3, 2021
93f970c
Update README.md
kmjennison Aug 3, 2021
2671a1e
Merge pull request #261 from gladly-team/kevin/dynamic-redirect-docs
kmjennison Aug 3, 2021
e6ee173
Create CODE_OF_CONDUCT.md
kmjennison Aug 13, 2021
d87b5f5
Update README.md
kmjennison Aug 13, 2021
1ca3b4c
Create CONTRIBUTING.md
kmjennison Aug 13, 2021
f15721c
Update CONTRIBUTING.md
kmjennison Aug 13, 2021
ea9184b
Update CONTRIBUTING.md
kmjennison Aug 13, 2021
b48a351
Update README.md
kmjennison Aug 13, 2021
a418081
Update CONTRIBUTING.md
kmjennison Aug 13, 2021
0ceccd0
Merge pull request #271 from gladly-team/kevin/contributing-docs
kmjennison Aug 13, 2021
465b902
Update README.md
kmjennison Aug 13, 2021
c4dc739
Update comment
kmjennison Aug 16, 2021
d9f0204
0.13.3
kmjennison Aug 16, 2021
0d004d9
Update example app
kmjennison Aug 16, 2021
23276ce
Merge pull request #277 from gladly-team/kevin/upgrade-example
kmjennison Aug 16, 2021
647b603
#142: TypeScript and `withAuthUser`
Aug 24, 2021
6c16524
WIP
kmjennison Aug 27, 2021
131219c
Use type in server-side data fetch
kmjennison Aug 27, 2021
161c102
Merge pull request #289 from gladly-team/kevin/typescript-example
kmjennison Aug 27, 2021
0c3b278
Merge pull request #283 from gavinharris-dev/main
kmjennison Aug 27, 2021
ee3d770
Update README.md
kmjennison Aug 27, 2021
b46fd40
Update README.md
kmjennison Aug 27, 2021
39cd166
Merge pull request #290 from gladly-team/kevin/typescript-readme
kmjennison Aug 27, 2021
581ef6b
Debug release action
kmjennison Aug 29, 2021
1e04c95
0.14.0-alpha.0
kmjennison Aug 29, 2021
a6ec037
Revert "0.14.0-alpha.0"
kmjennison Aug 29, 2021
a813889
Remove --dry-run from release action
kmjennison Aug 29, 2021
e11fdd3
Display the Firebase version on the demo app
kmjennison Aug 30, 2021
92bdc83
Merge pull request #294 from gladly-team/kevin/demo-firebase-version
kmjennison Aug 30, 2021
d101b30
Update README.md
kmjennison Aug 31, 2021
d99a9a6
Update issue templates
kmjennison Sep 2, 2021
2e11b45
feat: forceRefresh on getIdToken
iamgbayer Sep 17, 2021
0bebb9a
feat: adding description about forceRefresh
iamgbayer Sep 17, 2021
a344a86
feat: adding argument type to getIdToken
iamgbayer Sep 17, 2021
3dce8ed
Update createAuthUser.js
kmjennison Sep 17, 2021
5f36c0d
Merge pull request #301 from iamgbayer/guilhermebayer/adding-force-re…
kmjennison Sep 17, 2021
f429935
0.13.4-alpha.0
kmjennison Sep 17, 2021
7ad6080
0.13.4
kmjennison Oct 15, 2021
49d4e4f
Upgrade some dependencies (#325)
kmjennison Nov 2, 2021
1e0ec3f
Support Next 12 and Firebase Admin 10 (#328)
kmjennison Nov 2, 2021
dd17d66
0.13.5-alpha.0
kmjennison Nov 2, 2021
887eb76
Update demo to use Next 12 (#330)
kmjennison Nov 2, 2021
c209e49
Merge branch 'main' of https://github.com/gladly-team/next-firebase-a…
Nov 5, 2021
304334c
fix
Nov 5, 2021
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
9 changes: 6 additions & 3 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ assignees: ''
---

**Describe the bug**
A clear and concise description of what the bug is.
A clear and concise description of the bug.

**Version**
What version of `next-firebase-auth` are you using?
**Versions**

`next-firebase-auth` version:
Firebase JS SDK:
Next.js:

**To Reproduce**
Steps to reproduce the behavior:
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ jobs:
- run: yarn install
- run: yarn test
- run: yarn run build
- run: npm publish
- run: echo ${{github.ref}}
- name: Release canary version
# The ref will be the tag name. Match against tags like
# "v1.0.0-canary.0".
if: ${{ contains(github.ref, '-canary.') }}
run: npm publish --tag canary
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
- name: Release latest version
# Assume non-canary is latest.
if: ${{ !contains(github.ref, '-canary.') }}
run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
1 change: 0 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"bracketSpacing": true,
"jsxBracketSameLine": false,
"useTabs": false,
"semi": false,
"singleQuote": true,
Expand Down
40 changes: 40 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# Contributor Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, caste, color, religion, or sexual identity and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.

## Our Standards

Examples of behavior that contributes to a positive environment for our community include:

* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
* Focusing on what is best not just for us as individuals, but for the overall community

Examples of unacceptable behavior include:

* The use of sexualized language or imagery, and sexual attention or advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others’ private information, such as a physical or email address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.

Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.

## Scope

This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant, version 2.1](https://www.contributor-covenant.org/version/2/1/code_of_conduct/).
55 changes: 55 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Contributing

Welcome, and thanks for considering contributing to `next-firebase-auth`! We really appreciate your support.

Here are some quick guidelines on how to contribute productively.

### Open issues before working

Before working on a new feature or bug fix, please open a detailed issue for discussion. This helps prevent wasted time (for example, if the feature is out of scope of this package) and gives other community members a chance for input.

If you'll start work on an existing issue, please comment that you're working on it so other contributors don't duplicate efforts.

### Use issue templates

Please use the issue templates. Issues outside of the template guidelines may be closed.

Use discussions for Q&A and for help on your app's specific implementation.

If reporting a bug, especially a full-stack one (such as problems with cookies), please provide a full example or clear way to reproduce it.

### Security disclosures

If you find a security vulnerability, do **not** open an issue. Please email the maintainers at [[email protected]](mailto:[email protected]).

### Making code contributions

1. Before coding, please open a related issue (or comment on an existing issue to let us know you're working on it)
2. Fork this repository
3. Make code changes in your fork
* Add comments where it's potentially unclear what your code is doing
* Commit with descriptive messages
* Ensure complete code coverage for your changes
* Make sure all linting and tests succeed (`yarn run test`)
4. Open a pull request pointing to the `main` branch in this repository
* Add a clear title
* In the description, link to the related issue, such as: `Closes #123.`

### Developing with a local version of the package

While developing, it can be helpful to use a local version of `next-firebase-auth` in another app. To do so:

1. Install [yalc](https://www.npmjs.com/package/yalc): `yarn global add yalc`
2. In `next-firebase-auth`, publish a local version: `yarn run dev:publish` -- this builds your local package code, then publishes it with Yalc
3. In another local Next.js app, such as the example app in this repository: `yalc add next-firebase-auth`
4. After you make changes to your local `next-firebase-auth`, use `yarn run dev:publish` again to use the latest local code in your app

### We may be slow to respond

At the moment, this repository is maintained by ~1 person in their spare time. We will be as responsive as possible, but we may be slow to respond to issues and PRs. Thanks for your patience!

### Be a good community member

Please respect our [Code of Conduct](./CODE_OF_CONDUCT.md) and contribute to a positive, constructive open source environment.

We strive to support first-time contributors and beginners through guidance and constructive feedback.
134 changes: 116 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[![Build Status](https://img.shields.io/github/workflow/status/gladly-team/next-firebase-auth/Unit%20test,%20log%20code%20coverage,%20and%20build)](https://github.com/gladly-team/next-firebase-auth/actions?query=workflow%3A%22Unit+test%2C+log+code+coverage%2C+and+build%22)
[![codecov](https://codecov.io/gh/gladly-team/next-firebase-auth/branch/main/graph/badge.svg)](https://codecov.io/gh/gladly-team/next-firebase-auth)
[![npm](https://img.shields.io/npm/v/next-firebase-auth.svg)](https://www.npmjs.com/package/next-firebase-auth)
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](./CODE_OF_CONDUCT.md)

# next-firebase-auth
Simple Firebase authentication for all Next.js rendering strategies.

#### [Demo](#demo) • [Alternatives](#when-not-to-use-this-package) • [Getting Started](#get-started) • [API](#api) • [Config](#config) • [Types](#types) • [Examples](#examples) • [Troubleshooting](#troubleshooting)
#### [Demo](#demo) • [Alternatives](#when-not-to-use-this-package) • [Getting Started](#get-started) • [API](#api) • [Config](#config) • [Types](#types) • [Examples](#examples) • [Troubleshooting](#troubleshooting) • [Contributing](./CONTRIBUTING.md)

## What It Does
This package makes it simple to get the authenticated Firebase user and ID token during both client-side and server-side rendering (SSR).
Expand Down Expand Up @@ -49,6 +50,8 @@ Install:

`yarn add next-firebase-auth`

> ⚠️ If you're using v9 of the Firebase JS SDK, use `next-firebase-auth@canary`. This is an unstable v1 prerelease. Track progress on v1 [in this issue](https://github.com/gladly-team/next-firebase-auth/issues/265).

Make sure peer dependencies are also installed:

`yarn add firebase firebase-admin next react react-dom`
Expand Down Expand Up @@ -221,8 +224,8 @@ Option | Description | Default
`whenAuthed` | The action to take if the user is authenticated. One of `AuthAction.RENDER` or `AuthAction.REDIRECT_TO_APP`. | `AuthAction.RENDER`
`whenUnauthedBeforeInit` | The action to take if the user is *not* authenticated but the Firebase client JS SDK has not yet initialized. One of: `AuthAction.RENDER`, `AuthAction.REDIRECT_TO_LOGIN`, `AuthAction.SHOW_LOADER`. | `AuthAction.RENDER`
`whenUnauthedAfterInit` | The action to take if the user is *not* authenticated and the Firebase client JS SDK has already initialized. One of: `AuthAction.RENDER`, `AuthAction.REDIRECT_TO_LOGIN`. | `AuthAction.RENDER`
`appPageURL` | The redirect destination URL when we should redirect to the app. | `config.appPageURL`
`authPageURL` | The redirect destination URL when we should redirect to the login page. | `config.authPageURL`
`appPageURL` | The redirect destination URL when we should redirect to the app. Can be a string or a function that receives `{ ctx }` and returns a URL. | `config.appPageURL`
`authPageURL` | The redirect destination URL when we should redirect to the login page. Can be a string or a function that receives `{ ctx }` and returns a URL. | `config.authPageURL`
`LoaderComponent` | The component to render when the user is unauthed and `whenUnauthedBeforeInit` is set to `AuthAction.SHOW_LOADER`. | null

For example, this page will redirect to the login page if the user is not authenticated:
Expand Down Expand Up @@ -253,6 +256,8 @@ export default withAuthUser({
})(LoginPage)
```

For TypeScript usage, take a look [here](#typescript).

#### `withAuthUserTokenSSR({ ...options })(getServerSidePropsFunc = ({ AuthUser }) => {})`

A higher-order function that wraps a Next.js pages's `getServerSideProps` function to provide the `AuthUser` context during server-side rendering. Optionally, it can server-side redirect based on the user's auth status. A wrapped function is optional; if provided, it will be called with a `context` object that contains an [`AuthUser`](#authuser) property.
Expand All @@ -263,8 +268,8 @@ Option | Description | Default
------------ | ------------- | -------------
`whenAuthed` | The action to take if the user is authenticated. Either `AuthAction.RENDER` or `AuthAction.REDIRECT_TO_APP`. | `AuthAction.RENDER`
`whenUnauthed` | The action to take if the user is *not* authenticated. Either `AuthAction.RENDER` or `AuthAction.REDIRECT_TO_LOGIN`. | `AuthAction.RENDER`
`appPageURL` | The redirect destination URL when we should redirect to the app. | `config.appPageURL`
`authPageURL` | The redirect destination URL when we should redirect to the login page. | `config.authPageURL`
`appPageURL` | The redirect destination URL when we should redirect to the app. Can be a string or a function that receives `{ ctx }` and returns a URL. | `config.appPageURL`
`authPageURL` | The redirect destination URL when we should redirect to the login page. Can be a string or a function that receives `{ ctx }` and returns a URL. | `config.authPageURL`


For example, this page will SSR for authenticated users, fetching props using their Firebase ID token, and will server-side redirect to the login page if the user is not authenticated:
Expand Down Expand Up @@ -394,9 +399,9 @@ export default withAuthUser()(Artist)

See an [example config here](#example-config). Provide the config when you call `init`.

**authPageURL**: The default URL to navigate to when `withAuthUser` or `withAuthUserTokenSSR` need to redirect to login. Optional unless using the `AuthAction.REDIRECT_TO_LOGIN` auth action.
**authPageURL**: The default URL to navigate to when `withAuthUser` or `withAuthUserTokenSSR` need to redirect to login. Can be a string or a function that receives `{ ctx }` and returns a URL. Optional unless using the `AuthAction.REDIRECT_TO_LOGIN` auth action.

**appPageURL**: The default URL to navigate to when `withAuthUser` or `withAuthUserTokenSSR` need to redirect to the app. Optional unless using the `AuthAction.REDIRECT_TO_APP` auth action.
**appPageURL**: The default URL to navigate to when `withAuthUser` or `withAuthUserTokenSSR` need to redirect to the app. Can be a string or a function that receives `{ ctx }` and returns a URL. Optional unless using the `AuthAction.REDIRECT_TO_APP` auth action.

**loginAPIEndpoint**: The API endpoint this module will call when the auth state changes for an authenticated Firebase user. Must be set unless `tokenChangedHandler` is set.

Expand Down Expand Up @@ -527,6 +532,10 @@ The user from the Firebase JS SDK, if it has initialized. Otherwise, null.
A method that calls Firebase's [`signOut`](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signout) if the Firebase JS SDK has initialized. If the SDK has not initialized, this method is a noop.

## Examples
* [Using the Firebase Apps](#using-the-firebase-apps)
* [TypeScript](#typescript)
* [Dynamic Redirects](#dynamic-redirects)
* [Testing and Mocking with Jest](#testing-and-mocking-with-jest)

### Using the Firebase Apps

Expand Down Expand Up @@ -565,6 +574,104 @@ const Artists = () => {
}
```


### TypeScript

When using `withAuthUser` with TypeScript, use [TypeScript Generics](https://www.typescriptlang.org/docs/handbook/2/generics.html). For example:

```TypeScript
// /pages/demo.tsx
import { VFC } from 'react'
import { Loading } from 'components/Loading/Loading'
import { AuthAction, withAuthUser } from 'next-firebase-auth'

type DemoDataType = {
name: string
}

const Demo: VFC<DemoDataType> = ({ name }) => {
return <div>Hello {name}!</div>
}

export default withAuthUser<DemoDataType>({ // <--- Ensure that the type is provided
whenUnauthedBeforeInit: AuthAction.SHOW_LOADER,
whenUnauthedAfterInit: AuthAction.REDIRECT_TO_LOGIN,
LoaderComponent: Loading,
})(Demo)
```

For a full example with server-side data fetching, see the [TypeScript demo page](https://github.com/gladly-team/next-firebase-auth/blob/main/example/pages/ssr-no-token.tsx) in the example app.

### Dynamic Redirects

This package makes it easy to redirect to a login page or app page depending on whether a user is logged in. The destination URLs can also be dynamic: the `authPageURL` and `appPageURL` properties, used in the config and higher-order components, can be functions that receive `{ ctx }` and return a URL.

The [example app](https://github.com/gladly-team/next-firebase-auth/tree/main/example) uses this to set a post-login destination URL:

```js
// ./utils/initAuth.js
import { init } from 'next-firebase-auth'
import absoluteUrl from 'next-absolute-url'

const initAuth = () => init({
// This demonstrates setting a dynamic destination URL when
// redirecting from app pages. Alternatively, you can simply
// specify `authPageURL: '/auth-ssr'`.
authPageURL: ({ ctx }) => {
const isServerSide = typeof window === 'undefined'
const origin = isServerSide
? absoluteUrl(ctx.req).origin
: window.location.origin
const destPath =
typeof window === 'undefined' ? ctx.resolvedUrl : window.location.href
const destURL = new URL(destPath, origin)
return `auth-ssr?destination=${encodeURIComponent(destURL)}`
},

// This demonstrates setting a dynamic destination URL when
// redirecting from auth pages. Alternatively, you can simply
// specify `appPageURL: '/'`.
appPageURL: ({ ctx }) => {
const isServerSide = typeof window === 'undefined'
const origin = isServerSide
? absoluteUrl(ctx.req).origin
: window.location.origin
const params = isServerSide
? new URL(ctx.req.url, origin).searchParams
: new URLSearchParams(window.location.search)
const destinationParamVal = params.get('destination')
? decodeURIComponent(params.get('destination'))
: undefined

// By default, go to the index page if the destination URL
// is invalid or unspecified.
let destURL = '/'
if (destinationParamVal) {
// Verify the redirect URL host is allowed.
// https://owasp.org/www-project-web-security-testing-guide/v41/4-Web_Application_Security_Testing/11-Client_Side_Testing/04-Testing_for_Client_Side_URL_Redirect
const allowedHosts = ['localhost:3000', 'nfa-example.vercel.app']
const allowed =
allowedHosts.indexOf(new URL(destinationParamVal).host) > -1
if (allowed) {
destURL = destinationParamVal
} else {
// eslint-disable-next-line no-console
console.warn(
`Redirect destination host must be one of ${allowedHosts.join(
', '
)}.`
)
}
}
return destURL
},

// ... other config
}

export default initAuth
```

### Testing and Mocking with Jest

In order to test components wrapped with functions from `next-firebase-auth`, you will likely want to mock the `next-firebase-auth` library. This can be achieved using the [manual mocks feature of Jest](https://jestjs.io/docs/manual-mocks#mocking-node-modules).
Expand Down Expand Up @@ -795,15 +902,6 @@ We expect some apps will need some features that are not currently available:

We'd love to hear your feedback on these or other features. Please feel free to [open a discussion](https://github.com/gladly-team/next-firebase-auth/discussions)!

## Developing / Contributing

We welcome contributions! Please feel free to jump into any open issues.

### Using a local version of the package

It can be helpful to use an in-development version of `next-firebase-auth` in another app:
## Contributing

1. Install [yalc](https://www.npmjs.com/package/yalc): `yarn global add yalc`
2. In `next-firebase-auth`, publish a local version: `yarn run dev:publish` -- this builds your local package code, then publishes it with Yalc
3. In another local Next.js app: `yalc add next-firebase-auth`
4. After you make changes to your local `next-firebase-auth`, use `yarn run dev:publish` again to use the latest local code in your app
We welcome contributions! Please see [contributing docs](./CONTRIBUTING.md) to get started.
3 changes: 0 additions & 3 deletions example/components/DemoPageLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ const DemoPageLinks = () => (
<Link href="/ssr-no-token">
<a style={styles.linkAnchor}>Example: SSR + no ID token</a>
</Link>
<Link href="/ssr-custom-routing">
<a style={styles.linkAnchor}>Example: SSR + custom routing</a>
</Link>
<Link href="/static-auth-required-loader">
<a style={styles.linkAnchor}>
Example: static + loader + data fetching with ID token
Expand Down
4 changes: 2 additions & 2 deletions example/components/FirebaseAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const firebaseAuthConfig = {
callbacks: {
// https://github.com/firebase/firebaseui-web#signinsuccesswithauthresultauthresult-redirecturl
signInSuccessWithAuthResult: () =>
// Don't automatically redirect. We handle redirecting based on
// auth state in withAuthComponent.js.
// Don't automatically redirect. We handle redirects using
// `next-firebase-auth`.
false,
},
}
Expand Down
8 changes: 5 additions & 3 deletions example/components/Header.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react'
import Link from 'next/link'

const nfaDependencyVersion = require('../package.json').dependencies[
'next-firebase-auth'
]
const nfaDependencyVersion =
require('../package.json').dependencies['next-firebase-auth']
const nextDependencyVersion = require('../package.json').dependencies.next
const firebaseDependencyVersion =
require('../package.json').dependencies.firebase

const styles = {
container: {
Expand All @@ -28,6 +29,7 @@ const Header = ({ email, signOut }) => (
<div style={styles.versionsContainer}>
<div>v{nfaDependencyVersion}</div>
<div>Next.js v{nextDependencyVersion}</div>
<div>Firebase v{firebaseDependencyVersion}</div>
</div>
{email ? (
<>
Expand Down
Loading