Skip to content

Commit a59d3d3

Browse files
committed
docs: write a dedicated guide for path logic
1 parent 3aac998 commit a59d3d3

File tree

4 files changed

+146
-33
lines changed

4 files changed

+146
-33
lines changed

docs/.vitepress/routes/sidebar/guide.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export const routes: DefaultTheme.SidebarItem[] = [
9696
],
9797
},
9898
{ text: 'Caching', link: '/caching' },
99+
{ text: 'Pathing logic and baseURL', link: '/url-resolutions' },
99100
],
100101
},
101102
]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Pathing logic in NuxtAuth
2+
3+
This page is here to clarify how the pathing logic works in `@sidebase/nuxt-auth`.
4+
You can find a full overview of how URLs are handled [in the issue comment](https://github.com/sidebase/nuxt-auth/pull/913#issuecomment-2359137989) and in spec files for [`authjs` provider](https://github.com/sidebase/nuxt-auth/blob/main/tests/authjs.url.spec.ts) and [`local` provider](https://github.com/sidebase/nuxt-auth/blob/main/tests/local.url.spec.ts).
5+
6+
## `baseURL` is a prefix
7+
8+
It will be prepended to a path before making a call. For example,
9+
10+
```ts
11+
export default defineNuxtConfig({
12+
auth: {
13+
baseURL: 'https://example.com/api/auth',
14+
15+
provider: {
16+
type: 'local',
17+
endpoints: {
18+
// The call would be made to `https://example.com/api/auth/login`
19+
signIn: { path: '/login', method: 'post' },
20+
}
21+
}
22+
}
23+
})
24+
```
25+
26+
## Use URL provided in `endpoints` if it is fully specified
27+
28+
If you provide a full URL to `endpoints`, it will be used when making calls to an endpoint:
29+
30+
```ts {9}
31+
export default defineNuxtConfig({
32+
auth: {
33+
baseURL: 'https://your.website/api',
34+
35+
provider: {
36+
type: 'local',
37+
endpoints: {
38+
// This will call `https://example.com/user`
39+
getSession: { path: 'https://example.com/user' },
40+
41+
// This will call `https://your.website/api/login`
42+
signIn: { path: '/login', method: 'post' },
43+
},
44+
}
45+
}
46+
})
47+
```
48+
49+
## `runtimeConfig`
50+
51+
Value of `baseURL` is always located at `runtimeConfig.public.auth.baseURL`. You cannot change it directly as of the moment of writing, but you can read the value in your application:
52+
53+
```ts
54+
const runtimeConfig = useRuntimeConfig()
55+
const baseURL = runtimeConfig.public.auth.baseURL
56+
```
57+
58+
This value is generally the [source of truth](https://github.com/sidebase/nuxt-auth/blob/b5af548c1fc390ae00496e19ad7a91d308af9b12/src/runtime/utils/url.ts#L37-L38). It is being [set in the plugin](https://github.com/sidebase/nuxt-auth/blob/b5af548c1fc390ae00496e19ad7a91d308af9b12/src/runtime/plugin.ts#L20-L24) to also be available on the client.
59+
60+
## Changing `baseURL`
61+
62+
Read next to understand how it can be changed.
63+
64+
### 1. Environment variables
65+
66+
You have multiple ways of changing the `baseURL` via env variables:
67+
- use `NUXT_PUBLIC_AUTH_BASE_URL`;
68+
- use `AUTH_ORIGIN` if `originEnvKey` is not set;
69+
- use the environment variable name set in [`originEnvKey`](/guide/application-side/configuration#originenvkey)
70+
71+
Environment variables should work in both build-time and runtime.
72+
73+
### 2. `baseURL`
74+
75+
If you didn't set an environment variable, NuxtAuth will look for [`auth.baseURL`](/guide/application-side/configuration#baseurl) inside the `nuxt.config.ts`.
76+
77+
Note that this variable is always **static**, will only be set during runtime and can still be overriden in runtime using env variables.
78+
79+
Not setting `baseURL` will default to `/api/auth`.
80+
81+
### 3. `authjs` only: determine origin automatically from the incoming `HTTP` request
82+
83+
When the server is running in **development mode**, NuxtAuth can automatically infer `baseURL` from the incoming request.
84+
85+
---
86+
87+
Therefore as of version 0.10.0, we recommend the following setup to set your `AUTH_ORIGIN` or `baseURL`:
88+
89+
::: code-group
90+
91+
```ts diff [nuxt.config.ts]
92+
export default defineNuxtConfig({
93+
// ... other configuration
94+
auth: {
95+
baseUrl: 'https://my-backend.com/api/auth', // [!code --]
96+
// This is technically not needed as it is the default, but it's here for illustrative purposes
97+
originEnvKey: 'AUTH_ORIGIN', // [!code ++]
98+
}
99+
})
100+
```
101+
102+
```env diff [.env]
103+
AUTH_ORIGIN="https://my-backend.com/api/auth" // [!code ++]
104+
```
105+
106+
:::

docs/guide/local/quick-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ In the example above, we define a [runtime config](https://nuxt.com/docs/guide/g
5353

5454
You can customize each endpoint to fit your needs or disable it by setting it to `false`. For example you may want to disable the `signUp` endpoint.
5555

56-
```ts{7}
56+
```ts{6}
5757
export default defineNuxtConfig({
5858
auth: {
5959
provider: {

docs/upgrade/version-0.10.0.md

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,48 +43,33 @@ definePageMeta({
4343

4444
### Adjustments to the computation of `baseURL` and `AUTH_ORIGIN`
4545

46-
In 0.10.0 we spent a considerable amount of time reworking how `@sidebase/nuxt-auth` determine which endpoints to make requests to. If you would like to view the full PR and discussion, you can find it [here](https://github.com/sidebase/nuxt-auth/pull/913).
46+
In 0.10.0 we spent a considerable amount of time reworking how `@sidebase/nuxt-auth` determines which endpoints to make requests to. If you would like to view the full PR and discussion, you can find it [here](https://github.com/sidebase/nuxt-auth/pull/913).
4747

48-
As a quick overview, `@sidebase/nuxt-auth` will now use the following logic to determine where to make requests to:
48+
Read more [in a dedicated guide](../guide/advanced/url-resolutions.md).
4949

50-
#### 1. Environment variable and `runtimeConfig`
50+
Below are some notable changes.
5151

52-
Use the `AUTH_ORIGIN` environment variable or `runtimeConfig.authOrigin` if set. Name can be customized, refer to [`originEnvKey`](/guide/application-side/configuration#originenvkey).
52+
#### URLs are now joined
5353

54-
#### 2. `baseURL`
54+
`baseURL` now means exactly that, it will be prepended to a path before making a call. That means you need to adjust your config accordingly:
5555

56-
Use the static [`baseURL`](/guide/application-side/configuration#baseurl) inside the `nuxt.config.ts` if set.
57-
58-
#### 3. Automatically from the incoming `HTTP` request
59-
60-
When the server is running in **development mode**, NuxtAuth can automatically infer it from the incoming request.
61-
62-
---
63-
64-
Therefore as of version 0.10.0, we recommend the following setup to set your `AUTH_ORIGIN` or `baseURL`:
65-
66-
::: code-group
67-
68-
```ts diff [nuxt.config.ts]
56+
```ts diff
6957
export default defineNuxtConfig({
70-
// ...Previous configuration
71-
runtimeConfig: { // [!code ++]
72-
baseURL: '/api/auth' // [!code ++]
73-
}, // [!code ++]
7458
auth: {
75-
baseUrl: 'https://my-backend.com/api/auth', // [!code --]
76-
originEnvKey: 'NUXT_BASE_URL', // [!code ++]
59+
baseURL: 'https://example.com', // [!code --]
60+
baseURL: 'https://example.com/api/auth', // [!code ++]
61+
62+
provider: {
63+
type: 'local',
64+
endpoints: {
65+
signIn: { path: '/login', method: 'post' },
66+
}
67+
}
7768
}
7869
})
7970
```
8071

81-
```env diff [.env]
82-
NUXT_BASE_URL="https://my-backend.com/api/auth" // [!code ++]
83-
```
84-
85-
:::
86-
87-
### Adjustments when using an external backend for the `local`-provider
72+
#### Adjustments when using an external backend for the `local`-provider
8873

8974
In previous versions of `@sidebase/nuxt-auth` a very specific setup was needed to ensure that external backends could properly be used for the `local` provider. In 0.10.0 we reworked the internal handling or URLs to make it more consistent across providers and configurations.
9075

@@ -106,7 +91,28 @@ export default defineNuxtConfig({
10691
})
10792
```
10893

109-
You can find a full overview of how `@sidebase/nuxt-auth` handles URLs [here](https://github.com/sidebase/nuxt-auth/pull/913#issuecomment-2359137989) and in spec files for [`authjs` provider](https://github.com/sidebase/nuxt-auth/blob/main/tests/authjs.url.spec.ts) and [`local` provider](https://github.com/sidebase/nuxt-auth/blob/main/tests/local.url.spec.ts).
94+
---
95+
96+
Therefore as of version 0.10.0, we recommend the following setup to set your `AUTH_ORIGIN` or `baseURL`:
97+
98+
::: code-group
99+
100+
```ts diff [nuxt.config.ts]
101+
export default defineNuxtConfig({
102+
// ... other configuration
103+
auth: {
104+
baseUrl: 'https://my-backend.com/api/auth', // [!code --]
105+
// This is technically not needed as it is the default, but it's here for illustrative purposes
106+
originEnvKey: 'AUTH_ORIGIN', // [!code ++]
107+
}
108+
})
109+
```
110+
111+
```env diff [.env]
112+
AUTH_ORIGIN="https://my-backend.com/api/auth" // [!code ++]
113+
```
114+
115+
:::
110116

111117
## Changelog
112118

0 commit comments

Comments
 (0)