Skip to content

Commit 4729b1f

Browse files
hywaxphoenix-ruzoey-kaiser
authored
feat(#836)!: unification and flexibility with AUTH_ORIGIN (#837)
* feat!: unification and flexibility with `AUTH_ORIGIN` * docs: using `AUTH_ORIGIN` * chore: rebuild with pnpm@6 * fix: extracting a variable from ENV * docs: update `originEnvKey` type description Co-authored-by: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com> * fix: install scule after resolve conflicts * Update docs/guide/application-side/configuration.md Co-authored-by: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com> * fix: broken lock after resolve conflict * Update src/runtime/server/services/utils.ts Co-authored-by: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com> * fix: typecheck * Update docs/resources/error-reference.md Co-authored-by: Zoey <zoeykaiser8@gmail.com> --------- Co-authored-by: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com> Co-authored-by: Zoey <zoeykaiser8@gmail.com>
1 parent 2e564da commit 4729b1f

File tree

8 files changed

+162
-19
lines changed

8 files changed

+162
-19
lines changed

docs/guide/advanced/deployment/self-hosted.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This guide will explain how you can self-host a Nuxt3 application running NuxtAu
66

77
When deploying the Auth.JS provider, the application must be informed what URL it is running at. This is to properly determine callback urls when navigating users to external OAuth providers. Depending on your setup, NuxtAuth allows you to set this value at either [**Runtime**](https://nuxt.com/docs/guide/going-further/hooks#app-hooks-runtime) or [**Buildtime**](https://nuxt.com/docs/guide/going-further/hooks#nuxt-hooks-build-time).
88

9-
- **Runtime:** Set the `AUTH_ORIGIN` environment variable.
9+
- **Runtime:** Set the `NUXT_AUTH_ORIGIN` environment variable.
1010
- **Buildtime:** Set the `baseURL`-config key inside the `nuxt.config.ts`
1111

1212
The origin consists out of:
@@ -15,17 +15,17 @@ The origin consists out of:
1515
- **host:** e.g., localhost, example.org, google.com
1616
- **port:** empty (implies `:80` for http and `:443` for https), :3000, :8888
1717

18-
An example of the `AUTH_ORIGIN` would be: `https://my-awesome-app.com`
18+
An example of the `NUXT_AUTH_ORIGIN` would be: `https://my-awesome-app.com`
1919

2020
:::info Origin Order
2121
When [attempting to determine the server origin](https://github.com/sidebase/nuxt-auth/blob/main/src/runtime/server/services/utils.ts#L11), NuxtAuth checks the available options in the following order:
22-
- **Prio 1**: Using `AUTH_ORIGIN`
22+
- **Prio 1**: Using `NUXT_AUTH_ORIGIN`
2323
- **Prio 2**: Using `baseURL`-config key from inside the `nuxt.config.ts`
2424
- **Prio 3**: Infer the origin _(Only in development)_
2525
:::
2626

2727
:::tip
28-
We recommend setting the `AUTH_ORIGIN` during runtime and leaving the `baseURL`-config key empty, to avoid using a potentially incorrect ORIGIN.
28+
We recommend setting the `NUXT_AUTH_ORIGIN` during runtime and leaving the `baseURL`-config key empty, to avoid using a potentially incorrect ORIGIN.
2929
:::
3030

3131
In addition to verifying that the origin is correctly set, also ensure that you have a secure [`secret` set in the NuxtAuthHandler](/guide/authjs/nuxt-auth-handler#secret).

docs/guide/application-side/configuration.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineNuxtConfig({
88
auth: {
99
isEnabled: true,
1010
disableServerSideAuth: false,
11+
originEnvKey: 'AUTH_ORIGIN',
1112
baseURL: 'http://localhost:3000/api/auth',
1213
provider: { /* your provider config */ },
1314
sessionRefresh: {
@@ -25,6 +26,13 @@ export default defineNuxtConfig({
2526

2627
Whether the module is enabled at all
2728

29+
## `originEnvKey`
30+
31+
- **Type**: `string`
32+
- **Default**: `AUTH_ORIGIN`
33+
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).
35+
2836
## `disableServerSideAuth`
2937

3038
- **Type**: `boolean`

docs/resources/error-reference.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ export default NuxtAuthHandler({
2121

2222
`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:
2323

24-
1. Use the `AUTH_ORIGIN` environment variable if it is set,
24+
1. Use the `NUXT_AUTH_ORIGIN` environment variable if it is set
2525
2. Development only: Determine the origin automatically from the incoming HTTP request
2626

27-
2827
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+
29+
```ts
30+
// file: nuxt.config.ts
31+
export default defineNuxtConfig({
32+
runtimeConfig: {
33+
authOrigin: 'https://example.org', // You can either set a default or leave it empty
34+
}
35+
36+
// ... rest of your config
37+
})
38+
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"knitwork": "^1.1.0",
3939
"nitropack": "^2.9.7",
4040
"requrl": "^3.0.2",
41+
"scule": "^1.3.0",
4142
"ufo": "^1.5.4"
4243
},
4344
"peerDependencies": {

pnpm-lock.yaml

Lines changed: 119 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type {
2727
const topLevelDefaults = {
2828
isEnabled: true,
2929
disableServerSideAuth: false,
30+
originEnvKey: 'AUTH_ORIGIN',
3031
sessionRefresh: {
3132
enablePeriodically: false,
3233
enableOnWindowFocus: true,

src/runtime/server/services/utils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { H3Event } from 'h3'
22
import getURL from 'requrl'
33
import { joinURL } from 'ufo'
4+
import { camelCase } from 'scule'
45
import { isProduction } from '../../helpers'
56
import { ERROR_MESSAGES } from './errors'
67
import { useRuntimeConfig } from '#imports'
@@ -9,14 +10,18 @@ import { useRuntimeConfig } from '#imports'
910
* Get `origin` and fallback to `x-forwarded-host` or `host` headers if not in production.
1011
*/
1112
export const getServerOrigin = (event?: H3Event): string => {
13+
const config = useRuntimeConfig()
14+
1215
// Prio 1: Environment variable
13-
const envOrigin = process.env.AUTH_ORIGIN
16+
const envOriginKey = config.public.auth.originEnvKey!
17+
const envOriginKeyCamelcase = camelCase(envOriginKey, { normalize: true })
18+
const envOrigin = (config[envOriginKeyCamelcase] ?? process.env[envOriginKey]) as string | undefined
1419
if (envOrigin) {
1520
return envOrigin
1621
}
1722

18-
// Prio 2: Runtime configuration
19-
const runtimeConfigOrigin = useRuntimeConfig().public.auth.computed.origin
23+
// Prio 2: Computed origin
24+
const runtimeConfigOrigin = config.public.auth.computed.origin
2025
if (runtimeConfigOrigin) {
2126
return runtimeConfigOrigin
2227
}

src/runtime/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,15 @@ export interface ModuleOptions {
405405
* @default false
406406
*/
407407
disableServerSideAuth?: boolean;
408+
/**
409+
* The name of the environment variable that holds the origin of the application. This is used to determine the full URL of the application in production.
410+
* As an example, if you set `NUXT_AUTH_ORIGIN=http://example.org` in your `.env` file, the module will use this to determine the full URL of the application.
411+
*
412+
* Find more about this in the documentation: https://auth.sidebase.io/resources/error-reference#auth-no-origin
413+
*
414+
* @default 'AUTH_ORIGIN'
415+
*/
416+
originEnvKey?: string
408417
/**
409418
* Full url at which the app will run combined with the path to authentication. You can set this differently depending on your selected authentication-provider:
410419
* - `authjs`: You must set the full URL, with origin and path in production. You can leave this empty in development

0 commit comments

Comments
 (0)