From 9852116a7d3f3be56f6fdc1cba8bdff747c4cbb8 Mon Sep 17 00:00:00 2001 From: Johannes Przymusinski Date: Thu, 12 Sep 2024 12:03:10 +0200 Subject: [PATCH 1/5] docs: correct location of session data type definitions (#904) --- docs/guide/local/session-data.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/guide/local/session-data.md b/docs/guide/local/session-data.md index 33398446..4dfbcc7c 100644 --- a/docs/guide/local/session-data.md +++ b/docs/guide/local/session-data.md @@ -7,11 +7,13 @@ export default defineNuxtConfig({ auth: { provider: { type: 'local', - sessionDataType: { - id: 'string | number', - firstName: 'string', - lastName: 'string' - } + session: { + dataType: { + id: 'string | number', + firstName: 'string', + lastName: 'string', + }, + }, } } }) @@ -36,11 +38,13 @@ export default defineNuxtConfig({ auth: { provider: { type: 'local', - sessionDataType: { - id: 'string | number', - firstName: 'string', - lastName: 'string', - subscriptions: '{ id: number, active: boolean}[]' + session: { + dataType: { + id: 'string | number', + firstName: 'string', + lastName: 'string', + subscriptions: '{ id: number, active: boolean }[]' + }, } } } From 734415b11a4691ab4777bdb3de0f66dc2a0686fc Mon Sep 17 00:00:00 2001 From: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com> Date: Fri, 13 Sep 2024 01:36:21 +0200 Subject: [PATCH 2/5] docs(#906): clarify `origin` documentation (#908) --- docs/guide/application-side/configuration.md | 22 ++++++++++++- docs/resources/error-reference.md | 34 ++++++++++++++------ 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/docs/guide/application-side/configuration.md b/docs/guide/application-side/configuration.md index f1345781..6294ea6c 100644 --- a/docs/guide/application-side/configuration.md +++ b/docs/guide/application-side/configuration.md @@ -31,7 +31,27 @@ Whether the module is enabled at all - **Type**: `string` - **Default**: `AUTH_ORIGIN` -The name of the environment variable that holds the origin of the application. This is used to determine the origin of your application in production. Read more [here](/resources/error-reference#auth-no-origin). +The name of the environment variable that holds the origin of the application. This is used to determine the origin of your application in production. + +By default, NuxtAuth will look at `AUTH_ORIGIN` environment variable and `runtimeConfig.authOrigin`. + +::: tip +If you want to use `runtimeConfig` and `NUXT_` prefixed environment variables, you need to make sure to also define the key inside `runtimeConfig`, +because otherwise Nuxt will not acknowledge your env variable ([issue #906](https://github.com/sidebase/nuxt-auth/issues/906), read more [here](https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables)). + +```ts +export default defineNuxtConfig({ + auth: { + originEnvKey: 'NUXT_YOUR_ORIGIN' + }, + runtimeConfig: { + yourOrigin: '' + } +}) +``` +::: + +You can read additional information on `origin` determining [here](/resources/error-reference#auth-no-origin). ## `disableServerSideAuth` diff --git a/docs/resources/error-reference.md b/docs/resources/error-reference.md index ce8f071d..3760cecf 100644 --- a/docs/resources/error-reference.md +++ b/docs/resources/error-reference.md @@ -19,20 +19,36 @@ export default NuxtAuthHandler({ ## AUTH_NO_ORIGIN -`AUTH_NO_ORIGIN` will appear as a warning message during development and be thrown as an error that stops the application during production. It is safe to ignore the development warning - it is only meant as a heads-up for your later production-deployment. `AUTH_NO_ORIGIN` occurs when the origin of your application was not set. NuxtAuth tries to find the origin of your application in the following order: +`AUTH_NO_ORIGIN` will appear as a warning message during development and be thrown as an error that stops the application during production. +It is safe to ignore the development warning - it is only meant as a heads-up for your later production-deployment. -1. Use the `NUXT_AUTH_ORIGIN` environment variable if it is set -2. Development only: Determine the origin automatically from the incoming HTTP request +`AUTH_NO_ORIGIN` occurs when the origin of your application was not set. +NuxtAuth attempts to find the origin of your application in the following order ([source](https://github.com/sidebase/nuxt-auth/blob/9852116a7d3f3be56f6fdc1cba8bdff747c4cbb8/src/runtime/server/services/utils.ts#L8-L34)): -The `origin` is important for callbacks that happen to a specific origin for `oauth` flows. Note that in order for (2) to work the `origin` already has to be set at build-time, i.e., when you run `npm run build` or `npm run generate` and it will lead to the `origin` being inside your app-bundle. +### 1. Environment variable and `runtimeConfig` + +Use the `AUTH_ORIGIN` environment variable or `runtimeConfig.authOrigin` if set. Name can be customized, refer to [`originEnvKey`](/guide/application-side/configuration#originenvkey). + +### 2. `baseURL` + +The `origin` is computed using `ufo` from the provided `baseURL`. See implementation [here](https://github.com/sidebase/nuxt-auth/blob/9852116a7d3f3be56f6fdc1cba8bdff747c4cbb8/src/runtime/helpers.ts#L9-L23). ```ts -// file: nuxt.config.ts export default defineNuxtConfig({ - runtimeConfig: { - authOrigin: 'https://example.org', // You can either set a default or leave it empty + auth: { + baseURL: `http://localhost:${process.env.PORT || 3000}` } - - // ... rest of your config }) ``` + +### 3. Development only: automatically from the incoming HTTP request + +When the server is running in development mode, NuxtAuth can automatically infer it from the incoming request. + +::: info +This is only done for your convenience - make sure to set a proper origin in production. +::: + +--- + +If there is no valid `origin` after the steps above, `AUTH_NO_ORIGIN` error is thrown in production. From c32f9b11bf5f545b1fff37450a5470d46595550e Mon Sep 17 00:00:00 2001 From: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com> Date: Fri, 13 Sep 2024 01:56:30 +0200 Subject: [PATCH 3/5] enh: provide a demo implementation of refresh provider (#901) --- .github/workflows/ci.yaml | 15 ++-- playground-local/app.vue | 4 +- playground-local/config/AuthRefreshHandler.ts | 17 ++++ playground-local/nuxt.config.ts | 14 +++- .../server/api/auth/login.post.ts | 80 ++++++++++++++++--- .../server/api/auth/refresh.post.ts | 76 ++++++++++++++++++ playground-local/server/api/auth/user.get.ts | 47 ++++++----- 7 files changed, 217 insertions(+), 36 deletions(-) create mode 100644 playground-local/config/AuthRefreshHandler.ts create mode 100644 playground-local/server/api/auth/refresh.post.ts diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d0dd9904..026adb55 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -26,7 +26,7 @@ jobs: uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VER }} - cache: 'pnpm' + cache: "pnpm" - name: Install deps and prepare types run: pnpm i && pnpm dev:prepare @@ -56,7 +56,7 @@ jobs: uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VER }} - cache: 'pnpm' + cache: "pnpm" - name: Install deps and prepare types run: pnpm i && pnpm dev:prepare @@ -82,7 +82,7 @@ jobs: uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VER }} - cache: 'pnpm' + cache: "pnpm" - name: Install deps run: pnpm i @@ -93,7 +93,12 @@ jobs: # Check building - run: pnpm build - - name: Run Playwright tests using Vitest + - name: Run Playwright tests using Vitest with refresh disabled + run: pnpm test:e2e + env: + NUXT_AUTH_REFRESH_ENABLED: false + + - name: Run Playwright tests using Vitest with refresh enabled run: pnpm test:e2e test-playground-authjs: @@ -113,7 +118,7 @@ jobs: uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VER }} - cache: 'pnpm' + cache: "pnpm" - name: Install deps run: pnpm i diff --git a/playground-local/app.vue b/playground-local/app.vue index 6ccd2788..1f770a6f 100644 --- a/playground-local/app.vue +++ b/playground-local/app.vue @@ -4,8 +4,8 @@ import { useAuth } from '#imports' const { signIn, token, refreshToken, data, status, lastRefreshedAt, signOut, getSession } = useAuth() -const username = ref('') -const password = ref('') +const username = ref('smith') +const password = ref('hunter2')