Skip to content

Commit 734415b

Browse files
authored
docs(#906): clarify origin documentation (#908)
1 parent 9852116 commit 734415b

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

docs/guide/application-side/configuration.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,27 @@ Whether the module is enabled at all
3131
- **Type**: `string`
3232
- **Default**: `AUTH_ORIGIN`
3333

34-
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).
34+
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.
35+
36+
By default, NuxtAuth will look at `AUTH_ORIGIN` environment variable and `runtimeConfig.authOrigin`.
37+
38+
::: tip
39+
If you want to use `runtimeConfig` and `NUXT_` prefixed environment variables, you need to make sure to also define the key inside `runtimeConfig`,
40+
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)).
41+
42+
```ts
43+
export default defineNuxtConfig({
44+
auth: {
45+
originEnvKey: 'NUXT_YOUR_ORIGIN'
46+
},
47+
runtimeConfig: {
48+
yourOrigin: ''
49+
}
50+
})
51+
```
52+
:::
53+
54+
You can read additional information on `origin` determining [here](/resources/error-reference#auth-no-origin).
3555

3656
## `disableServerSideAuth`
3757

docs/resources/error-reference.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,36 @@ export default NuxtAuthHandler({
1919

2020
## AUTH_NO_ORIGIN
2121

22-
`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:
22+
`AUTH_NO_ORIGIN` will appear as a warning message during development and be thrown as an error that stops the application during production.
23+
It is safe to ignore the development warning - it is only meant as a heads-up for your later production-deployment.
2324

24-
1. Use the `NUXT_AUTH_ORIGIN` environment variable if it is set
25-
2. Development only: Determine the origin automatically from the incoming HTTP request
25+
`AUTH_NO_ORIGIN` occurs when the origin of your application was not set.
26+
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)):
2627

27-
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.
28+
### 1. Environment variable and `runtimeConfig`
29+
30+
Use the `AUTH_ORIGIN` environment variable or `runtimeConfig.authOrigin` if set. Name can be customized, refer to [`originEnvKey`](/guide/application-side/configuration#originenvkey).
31+
32+
### 2. `baseURL`
33+
34+
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).
2835

2936
```ts
30-
// file: nuxt.config.ts
3137
export default defineNuxtConfig({
32-
runtimeConfig: {
33-
authOrigin: 'https://example.org', // You can either set a default or leave it empty
38+
auth: {
39+
baseURL: `http://localhost:${process.env.PORT || 3000}`
3440
}
35-
36-
// ... rest of your config
3741
})
3842
```
43+
44+
### 3. Development only: automatically from the incoming HTTP request
45+
46+
When the server is running in development mode, NuxtAuth can automatically infer it from the incoming request.
47+
48+
::: info
49+
This is only done for your convenience - make sure to set a proper origin in production.
50+
:::
51+
52+
---
53+
54+
If there is no valid `origin` after the steps above, `AUTH_NO_ORIGIN` error is thrown in production.

0 commit comments

Comments
 (0)