Skip to content

Commit

Permalink
Merge pull request #200 from awinogrodzki/feat/async-response-factory…
Browse files Browse the repository at this point in the history
…-in-refresh-credentials

feat: support for async response factory in refreshCredentials method
  • Loading branch information
awinogrodzki authored Jun 5, 2024
2 parents 2367a7e + 86cdd0a commit 89a082b
Show file tree
Hide file tree
Showing 11 changed files with 1,048 additions and 500 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ jobs:
with:
path: |
node_modules
key: ${{ runner.os }}-node_modules-cache-v1-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
key: ${{ runner.os }}-node_modules-cache-v2-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
restore-keys: |
${{ runner.os }}-node_modules-cache-v1-${{ hashFiles('./yarn.lock') }}-
${{ runner.os }}-node_modules-cache-v2-${{ hashFiles('./yarn.lock') }}-
- run: yarn install

build:
Expand All @@ -59,15 +59,15 @@ jobs:
with:
path: |
node_modules
key: ${{ runner.os }}-node_modules-cache-v1-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
key: ${{ runner.os }}-node_modules-cache-v2-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
- name: Cache build
uses: actions/cache@v3
with:
path: |
lib
key: ${{ runner.os }}-build-cache-v1-${{ github.ref_name }}-${{github.sha}}
key: ${{ runner.os }}-build-cache-v2-${{ github.ref_name }}-${{github.sha}}
restore-keys: |
${{ runner.os }}-build-cache-v1-${{ github.ref_name }}-
${{ runner.os }}-build-cache-v2-${{ github.ref_name }}-
- run: yarn build

lint:
Expand All @@ -86,7 +86,7 @@ jobs:
with:
path: |
node_modules
key: ${{ runner.os }}-node_modules-cache-v1-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
key: ${{ runner.os }}-node_modules-cache-v2-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
- run: yarn lint

tests:
Expand All @@ -105,11 +105,11 @@ jobs:
with:
path: |
node_modules
key: ${{ runner.os }}-node_modules-cache-v1-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
key: ${{ runner.os }}-node_modules-cache-v2-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
- name: Cache build
uses: actions/cache@v3
with:
path: |
lib
key: ${{ runner.os }}-build-cache-v1-${{ github.ref_name }}-${{github.sha}}
key: ${{ runner.os }}-build-cache-v2-${{ github.ref_name }}-${{github.sha}}
- run: yarn test
16 changes: 8 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ jobs:
with:
path: |
node_modules
key: ${{ runner.os }}-node_modules-cache-v1-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
key: ${{ runner.os }}-node_modules-cache-v2-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
restore-keys: |
${{ runner.os }}-node_modules-cache-v1-${{ hashFiles('./yarn.lock') }}-
${{ runner.os }}-node_modules-cache-v2-${{ hashFiles('./yarn.lock') }}-
- run: yarn install

build:
Expand All @@ -61,15 +61,15 @@ jobs:
with:
path: |
node_modules
key: ${{ runner.os }}-node_modules-cache-v1-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
key: ${{ runner.os }}-node_modules-cache-v2-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
- name: Cache build
uses: actions/cache@v3
with:
path: |
lib
key: ${{ runner.os }}-build-cache-v1-${{ github.ref_name }}-${{github.sha}}
key: ${{ runner.os }}-build-cache-v2-${{ github.ref_name }}-${{github.sha}}
restore-keys: |
${{ runner.os }}-build-cache-v1-${{ github.ref_name }}-
${{ runner.os }}-build-cache-v2-${{ github.ref_name }}-
- run: yarn build

semantic-release:
Expand All @@ -93,15 +93,15 @@ jobs:
with:
path: |
node_modules
key: ${{ runner.os }}-node_modules-cache-v1-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
key: ${{ runner.os }}-node_modules-cache-v2-${{ hashFiles('./yarn.lock') }}-${{github.sha}}
- name: Cache build
uses: actions/cache@v3
with:
path: |
lib
key: ${{ runner.os }}-build-cache-v1-${{ github.ref_name }}-${{github.sha}}
key: ${{ runner.os }}-build-cache-v2-${{ github.ref_name }}-${{github.sha}}
restore-keys: |
${{ runner.os }}-build-cache-v1-${{ github.ref_name }}-
${{ runner.os }}-build-cache-v2-${{ github.ref_name }}-
- name: Semantic Release
uses: cycjimmy/semantic-release-action@v4
Expand Down
28 changes: 22 additions & 6 deletions docs/pages/docs/usage/refresh-credentials.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ export const config = {
};
```

### refreshCredentials response callback options

| Name | Description |
| ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| headers | [Modified Request Headers](https://vercel.com/templates/next.js/edge-functions-modify-request-header). Passing Modified Request Headers make sure that `getTokens` will return fresh token within **a single request** |
| tokens | `VerifyTokenResult` object. It contains `idToken`, `refreshToken` and `decodedIdToken` values. |
### refreshCredentials

`refreshCredentials` can be useful if we want to update user credentials after some asynchronous action that affects user token structure (eg. a cron or event queue that updates custom claims).

Expand All @@ -91,6 +86,27 @@ Calling `refreshCredentials` does three things:
2. It allows developer to create a new `NextResponse` with [Modified Request Headers](https://vercel.com/templates/next.js/edge-functions-modify-request-header). Passing Modified Request Headers make sure that `getTokens` will return fresh token within **a single request**
3. It updates `NextResponse` with `Set-Cookie` headers containing latest credentials


The function has following signature:
```tsx
async function refreshCredentials(
request: NextRequest,
options: SetAuthCookiesOptions,
responseFactory: (options: {
headers: Headers;
tokens: VerifyTokenResult;
}) => NextResponse | Promise<NextResponse>
): Promise<NextResponse>
```

#### Response factory options

| Name | Description |
| ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| headers | [Modified Request Headers](https://vercel.com/templates/next.js/edge-functions-modify-request-header). Passing Modified Request Headers make sure that `getTokens` will return fresh token within **a single request** |
| tokens | `VerifyTokenResult` object. It contains `idToken`, `refreshToken` and `decodedIdToken` values. |


## Refresh auth cookies in API Route Handlers

Use `refreshNextResponseCookies` from `next-firebase-auth-edge/lib/next/cookies` to refresh authentication cookies after updating user token in [API Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers)
Expand Down
2 changes: 1 addition & 1 deletion examples/next-typescript-starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@types/react-dom": "18.0.9",
"firebase": "^10.4.0",
"firebase-admin": "^11.10.1",
"next": "^14.1.0",
"next": "14.2.3",
"next-firebase-auth-edge": "1.5.3",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
142 changes: 74 additions & 68 deletions examples/next-typescript-starter/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -513,55 +513,55 @@
dependencies:
lodash "^4.17.21"

"@next/env@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/env/-/env-14.1.0.tgz#43d92ebb53bc0ae43dcc64fb4d418f8f17d7a341"
integrity sha512-Py8zIo+02ht82brwwhTg36iogzFqGLPXlRGKQw5s+qP/kMNc4MAyDeEwBKDijk6zTIbegEgu8Qy7C1LboslQAw==

"@next/swc-darwin-arm64@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.1.0.tgz#70a57c87ab1ae5aa963a3ba0f4e59e18f4ecea39"
integrity sha512-nUDn7TOGcIeyQni6lZHfzNoo9S0euXnu0jhsbMOmMJUBfgsnESdjN97kM7cBqQxZa8L/bM9om/S5/1dzCrW6wQ==

"@next/swc-darwin-x64@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-14.1.0.tgz#0863a22feae1540e83c249384b539069fef054e9"
integrity sha512-1jgudN5haWxiAl3O1ljUS2GfupPmcftu2RYJqZiMJmmbBT5M1XDffjUtRUzP4W3cBHsrvkfOFdQ71hAreNQP6g==

"@next/swc-linux-arm64-gnu@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.1.0.tgz#893da533d3fce4aec7116fe772d4f9b95232423c"
integrity sha512-RHo7Tcj+jllXUbK7xk2NyIDod3YcCPDZxj1WLIYxd709BQ7WuRYl3OWUNG+WUfqeQBds6kvZYlc42NJJTNi4tQ==

"@next/swc-linux-arm64-musl@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.1.0.tgz#d81ddcf95916310b8b0e4ad32b637406564244c0"
integrity sha512-v6kP8sHYxjO8RwHmWMJSq7VZP2nYCkRVQ0qolh2l6xroe9QjbgV8siTbduED4u0hlk0+tjS6/Tuy4n5XCp+l6g==

"@next/swc-linux-x64-gnu@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.1.0.tgz#18967f100ec19938354332dcb0268393cbacf581"
integrity sha512-zJ2pnoFYB1F4vmEVlb/eSe+VH679zT1VdXlZKX+pE66grOgjmKJHKacf82g/sWE4MQ4Rk2FMBCRnX+l6/TVYzQ==

"@next/swc-linux-x64-musl@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.1.0.tgz#77077cd4ba8dda8f349dc7ceb6230e68ee3293cf"
integrity sha512-rbaIYFt2X9YZBSbH/CwGAjbBG2/MrACCVu2X0+kSykHzHnYH5FjHxwXLkcoJ10cX0aWCEynpu+rP76x0914atg==

"@next/swc-win32-arm64-msvc@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.1.0.tgz#5f0b8cf955644104621e6d7cc923cad3a4c5365a"
integrity sha512-o1N5TsYc8f/HpGt39OUQpQ9AKIGApd3QLueu7hXk//2xq5Z9OxmV6sQfNp8C7qYmiOlHYODOGqNNa0e9jvchGQ==

"@next/swc-win32-ia32-msvc@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.1.0.tgz#21f4de1293ac5e5a168a412b139db5d3420a89d0"
integrity sha512-XXIuB1DBRCFwNO6EEzCTMHT5pauwaSj4SWs7CYnME57eaReAKBXCnkUE80p/pAZcewm7hs+vGvNqDPacEXHVkw==

"@next/swc-win32-x64-msvc@14.1.0":
version "14.1.0"
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.1.0.tgz#e561fb330466d41807123d932b365cf3d33ceba2"
integrity sha512-9WEbVRRAqJ3YFVqEZIxUqkiO8l1nool1LmNxygr5HWF8AcSYsEpneUDhmjUVJEzO2A04+oPtZdombzzPPkTtgg==
"@next/env@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/env/-/env-14.2.3.tgz#d6def29d1c763c0afb397343a15a82e7d92353a0"
integrity sha512-W7fd7IbkfmeeY2gXrzJYDx8D2lWKbVoTIj1o1ScPHNzvp30s1AuoEFSdr39bC5sjxJaxTtq3OTCZboNp0lNWHA==

"@next/swc-darwin-arm64@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.3.tgz#db1a05eb88c0224089b815ad10ac128ec79c2cdb"
integrity sha512-3pEYo/RaGqPP0YzwnlmPN2puaF2WMLM3apt5jLW2fFdXD9+pqcoTzRk+iZsf8ta7+quAe4Q6Ms0nR0SFGFdS1A==

"@next/swc-darwin-x64@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-14.2.3.tgz#a3f8af05b5f9a52ac3082e66ac29e125ab1d7b9c"
integrity sha512-6adp7waE6P1TYFSXpY366xwsOnEXM+y1kgRpjSRVI2CBDOcbRjsJ67Z6EgKIqWIue52d2q/Mx8g9MszARj8IEA==

"@next/swc-linux-arm64-gnu@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.2.3.tgz#4e63f43879285b52554bfd39e6e0cc78a9b27bbf"
integrity sha512-cuzCE/1G0ZSnTAHJPUT1rPgQx1w5tzSX7POXSLaS7w2nIUJUD+e25QoXD/hMfxbsT9rslEXugWypJMILBj/QsA==

"@next/swc-linux-arm64-musl@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.2.3.tgz#ebdaed26214448b1e6f2c3e8b3cd29bfba387990"
integrity sha512-0D4/oMM2Y9Ta3nGuCcQN8jjJjmDPYpHX9OJzqk42NZGJocU2MqhBq5tWkJrUQOQY9N+In9xOdymzapM09GeiZw==

"@next/swc-linux-x64-gnu@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.2.3.tgz#19e3bcc137c3b582a1ab867106817e5c90a20593"
integrity sha512-ENPiNnBNDInBLyUU5ii8PMQh+4XLr4pG51tOp6aJ9xqFQ2iRI6IH0Ds2yJkAzNV1CfyagcyzPfROMViS2wOZ9w==

"@next/swc-linux-x64-musl@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.2.3.tgz#794a539b98e064169cf0ff7741b2a4fb16adec7d"
integrity sha512-BTAbq0LnCbF5MtoM7I/9UeUu/8ZBY0i8SFjUMCbPDOLv+un67e2JgyN4pmgfXBwy/I+RHu8q+k+MCkDN6P9ViQ==

"@next/swc-win32-arm64-msvc@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.2.3.tgz#eda9fa0fbf1ff9113e87ac2668ee67ce9e5add5a"
integrity sha512-AEHIw/dhAMLNFJFJIJIyOFDzrzI5bAjI9J26gbO5xhAKHYTZ9Or04BesFPXiAYXDNdrwTP2dQceYA4dL1geu8A==

"@next/swc-win32-ia32-msvc@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.3.tgz#7c1190e3f640ab16580c6bdbd7d0e766b9920457"
integrity sha512-vga40n1q6aYb0CLrM+eEmisfKCR45ixQYXuBXxOOmmoV8sYST9k7E3US32FsY+CkkF7NtzdcebiFT4CHuMSyZw==

"@next/swc-win32-x64-msvc@14.2.3":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.2.3.tgz#2be4e39ee25bfbd85be78eea17c0e7751dc4323c"
integrity sha512-Q1/zm43RWynxrO7lW4ehciQVj+5ePBhOK+/K2P7pLFX3JaJ/IZVC69SHidrmZSOkqz7ECIOhhy7XhAFG4JYyHA==

"@protobufjs/aspromise@^1.1.1", "@protobufjs/aspromise@^1.1.2":
version "1.1.2"
Expand Down Expand Up @@ -616,11 +616,17 @@
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==

"@swc/helpers@0.5.2":
version "0.5.2"
resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.5.2.tgz#85ea0c76450b61ad7d10a37050289eded783c27d"
integrity sha512-E4KcWTpoLHqwPHLxidpOqQbcrZVgi0rsmmZXUle1jXmJfuIf/UWpczUJ7MZZ5tlxytgJXyp0w4PGkkeLiuIdZw==
"@swc/counter@^0.1.3":
version "0.1.3"
resolved "https://registry.yarnpkg.com/@swc/counter/-/counter-0.1.3.tgz#cc7463bd02949611c6329596fccd2b0ec782b0e9"
integrity sha512-e2BR4lsJkkRlKZ/qCHPw9ZaSxc0MVUd7gtbtaB7aMvHeJVYe8sOB8DBZkP2DtISHGSku9sCK6T6cnY0CtXrOCQ==

"@swc/helpers@0.5.5":
version "0.5.5"
resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.5.5.tgz#12689df71bfc9b21c4f4ca00ae55f2f16c8b77c0"
integrity sha512-KGYxvIOXcceOAbEk4bi/dVLEK9z8sZ0uBB3Il5b1rhfClSpcX0yfRO0KmTkqR2cnQDymwLB+25ZyMzICg/cm/A==
dependencies:
"@swc/counter" "^0.1.3"
tslib "^2.4.0"

"@tootallnate/once@2":
Expand Down Expand Up @@ -1622,28 +1628,28 @@ next-firebase-auth-edge@1.5.3:
encoding "^0.1.13"
jose "^5.2.3"

next@^14.1.0:
version "14.1.0"
resolved "https://registry.yarnpkg.com/next/-/next-14.1.0.tgz#b31c0261ff9caa6b4a17c5af019ed77387174b69"
integrity sha512-wlzrsbfeSU48YQBjZhDzOwhWhGsy+uQycR8bHAOt1LY1bn3zZEcDyHQOEoN3aWzQ8LHCAJ1nqrWCc9XF2+O45Q==
next@14.2.3:
version "14.2.3"
resolved "https://registry.yarnpkg.com/next/-/next-14.2.3.tgz#f117dd5d5f20c307e7b8e4f9c1c97d961008925d"
integrity sha512-dowFkFTR8v79NPJO4QsBUtxv0g9BrS/phluVpMAt2ku7H+cbcBJlopXjkWlwxrk/xGqMemr7JkGPGemPrLLX7A==
dependencies:
"@next/env" "14.1.0"
"@swc/helpers" "0.5.2"
"@next/env" "14.2.3"
"@swc/helpers" "0.5.5"
busboy "1.6.0"
caniuse-lite "^1.0.30001579"
graceful-fs "^4.2.11"
postcss "8.4.31"
styled-jsx "5.1.1"
optionalDependencies:
"@next/swc-darwin-arm64" "14.1.0"
"@next/swc-darwin-x64" "14.1.0"
"@next/swc-linux-arm64-gnu" "14.1.0"
"@next/swc-linux-arm64-musl" "14.1.0"
"@next/swc-linux-x64-gnu" "14.1.0"
"@next/swc-linux-x64-musl" "14.1.0"
"@next/swc-win32-arm64-msvc" "14.1.0"
"@next/swc-win32-ia32-msvc" "14.1.0"
"@next/swc-win32-x64-msvc" "14.1.0"
"@next/swc-darwin-arm64" "14.2.3"
"@next/swc-darwin-x64" "14.2.3"
"@next/swc-linux-arm64-gnu" "14.2.3"
"@next/swc-linux-arm64-musl" "14.2.3"
"@next/swc-linux-x64-gnu" "14.2.3"
"@next/swc-linux-x64-musl" "14.2.3"
"@next/swc-win32-arm64-msvc" "14.2.3"
"@next/swc-win32-ia32-msvc" "14.2.3"
"@next/swc-win32-x64-msvc" "14.2.3"

node-fetch@2.6.7:
version "2.6.7"
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
'^.+\\.tsx?$': [
'ts-jest',
{
tsconfig: 'tsconfig.test.json',
tsconfig: 'tsconfig.json',
},
],
'^.+\\.jsx?$': 'babel-jest',
Expand Down
10 changes: 10 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
import "isomorphic-fetch";
import "dotenv/config";

declare module 'next/server' {
export interface NextRequest {
headers: Headers;
}

export interface NextResponse {
headers: Headers;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"jest": "^29.2.0",
"jest-fetch-mock": "^3.0.3",
"husky": "9.0.11",
"next": "^14.1.0",
"next": "14.2.3",
"prettier": "^2.6.2",
"ts-jest": "^29.0.3",
"typescript": "^4.9.3",
Expand Down
Loading

0 comments on commit 89a082b

Please sign in to comment.