You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/docs/configuration/initialization.md
+2-3
Original file line number
Diff line number
Diff line change
@@ -5,14 +5,14 @@ title: Initialization
5
5
6
6
The main entry point of NextAuth.js is the `NextAuth` method that you import from `next-auth`. It handles different types of requests, as defined in the [REST API](../getting-started/rest-api.md) section.
7
7
8
-
9
8
:::info
10
9
NextAuth.js cannot use the run [Edge Runtime](https://nextjs.org/docs/api-reference/edge-runtime) for initialization. The upcoming [`@auth/nextjs` library](https://authjs.dev/reference/nextjs) (which will replace `next-auth`) on the other hand will be fully compatible.
11
10
:::
12
11
13
12
You can initialize NextAuth.js in a few different ways.
14
13
15
14
## Simple initialization
15
+
16
16
### API Routes (`pages`)
17
17
18
18
In Next.js, you can define an API route that will catch all requests that begin with a certain path. Conveniently, this is called [Catch all API routes](https://nextjs.org/docs/api-routes/dynamic-api-routes#catch-all-api-routes).
@@ -62,7 +62,6 @@ Instead, `NextAuth` will receive the first two arguments of a Route Handler, and
62
62
63
63
If you have a specific use case and need to make NextAuth.js do something slightly different than what it is designed for, keep in mind, the `[...nextauth].ts` config file is just **a regular [API Route](https://nextjs.org/docs/api-routes/introduction)**.
64
64
65
-
66
65
That said, you can initialize NextAuth.js like this:
For more details on all available actions and which methods are supported, please check out the [REST API documentation](/getting-started/rest-api) or the appropriate area in [the source code](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/index.ts)
150
+
For more details on all available actions and which methods are supported, please check out the [REST API documentation](/getting-started/rest-api) or the appropriate area in [the source code](https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/core/index.ts)
152
151
153
152
This way of initializing `NextAuth` is very powerful, but should be used sparingly.
The following errors are passed as error query parameters to the default or overridden sign-in page:
46
46
47
-
-**OAuthSignin**: Error in constructing an authorization URL ([1](https://github.com/nextauthjs/next-auth/blob/457952bb5abf08b09861b0e5da403080cd5525be/src/server/lib/signin/oauth.js), [2](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/lib/oauth/pkce-handler.ts), [3](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/lib/oauth/state-handler.ts)),
48
-
-**OAuthCallback**: Error in handling the response ([1](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/lib/oauth/callback.ts), [2](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/lib/oauth/pkce-handler.ts), [3](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/lib/oauth/state-handler.ts)) from an OAuth provider.
47
+
-**OAuthSignin**: Error in constructing an authorization URL ([1](https://github.com/nextauthjs/next-auth/blob/457952bb5abf08b09861b0e5da403080cd5525be/src/server/lib/signin/oauth.js), [2](https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/core/lib/oauth/pkce-handler.ts), [3](https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/core/lib/oauth/state-handler.ts)),
48
+
-**OAuthCallback**: Error in handling the response ([1](https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/core/lib/oauth/callback.ts), [2](https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/core/lib/oauth/pkce-handler.ts), [3](https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/core/lib/oauth/state-handler.ts)) from an OAuth provider.
49
49
-**OAuthCreateAccount**: Could not create OAuth provider user in the database.
50
50
-**EmailCreateAccount**: Could not create email provider user in the database.
51
-
-**Callback**: Error in the [OAuth callback handler route](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/routes/callback.ts)
51
+
-**Callback**: Error in the [OAuth callback handler route](https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/core/routes/callback.ts)
52
52
-**OAuthAccountNotLinked**: If the email on the account is already linked, but not with this OAuth account
53
53
-**EmailSignin**: Sending the e-mail with the verification token failed
54
54
-**CredentialsSignin**: The `authorize` callback returned `null` in the [Credentials provider](/providers/credentials). We don't recommend providing information about which part of the credentials were wrong, as it might be abused by malicious hackers.
@@ -78,12 +78,17 @@ In addition, you can define a `theme.brandColor` to define a custom accent color
78
78
In order to get the available authentication providers and the URLs to use for them, you can make a request to the API endpoint `/api/auth/providers`:
// Note: Make sure not to redirect to the same page
105
110
// To avoid an infinite loop!
106
111
if (session) {
107
-
return { redirect: { destination: "/" } };
112
+
return { redirect: { destination: "/" } }
108
113
}
109
114
110
-
const providers =awaitgetProviders();
111
-
115
+
const providers =awaitgetProviders()
116
+
112
117
return {
113
118
props: { providers: providers?? [] },
114
119
}
@@ -122,10 +127,15 @@ There is another, more fully styled example signin page available [here](https:/
122
127
If you create a custom sign in form for email sign in, you will need to submit both fields for the **email** address and **csrfToken** from **/api/auth/csrf** in a POST request to **/api/auth/signin/email**.
If you create a sign in form for credentials based authentication, you will need to pass a **csrfToken** from **/api/auth/csrf** in a POST request to **/api/auth/callback/credentials**.
Copy file name to clipboardexpand all lines: docs/docs/configuration/providers/oauth.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -359,7 +359,7 @@ providers: [
359
359
360
360
## Built-in providers
361
361
362
-
NextAuth.js comes with a set of built-in providers. You can find them [here](https://github.com/nextauthjs/next-auth/tree/main/packages/next-auth/src/providers). Each built-in provider has its own documentation page:
362
+
NextAuth.js comes with a set of built-in providers. You can find them [here](https://github.com/nextauthjs/next-auth/tree/v4/packages/next-auth/src/providers). Each built-in provider has its own documentation page:
@@ -288,7 +283,7 @@ On the server side, **this is still available to use**, however, we recommend us
288
283
289
284
This helper is helpful in case you want to read the session outside of the context of React.
290
285
291
-
When called, `getSession()` will send a request to `/api/auth/session` and returns a promise with a [session object](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/types.ts#L407-L425), or `null` if no session exists.
286
+
When called, `getSession()` will send a request to `/api/auth/session` and returns a promise with a [session object](https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/core/types.ts#L407-L425), or `null` if no session exists.
292
287
293
288
```js
294
289
asyncfunctionmyFunction() {
@@ -520,7 +515,7 @@ where `data.url` is the validated URL you can redirect the user to without any f
520
515
## SessionProvider
521
516
522
517
:::note
523
-
If you are using the App Router, we encourage you to use [`getServerSession`](/configuration/nextjs#getserversession) in server contexts instead. (`SessionProvider`*can* be used in the App Router, which might be the easier choice if you are migrating from pages.)
518
+
If you are using the App Router, we encourage you to use [`getServerSession`](/configuration/nextjs#getserversession) in server contexts instead. (`SessionProvider`_can_ be used in the App Router, which might be the easier choice if you are migrating from pages.)
524
519
:::
525
520
526
521
Using the supplied `<SessionProvider>` allows instances of `useSession()` to share the session object across components, by using [React Context](https://react.dev/learn/passing-data-deeply-with-context) under the hood. It also takes care of keeping the session updated and synced between tabs/windows.
@@ -633,11 +628,13 @@ See [**the Next.js documentation**](https://nextjs.org/docs/advanced-features/cu
633
628
:::
634
629
635
630
### Custom base path
631
+
636
632
When your Next.js application uses a custom base path, set the `NEXTAUTH_URL` environment variable to the route to the API endpoint in full - as in the example below and as explained [here](/configuration/options#nextauth_url).
637
633
638
634
Also, make sure to pass the `basePath` page prop to the `<SessionProvider>` – as in the example below – so your custom base path is fully configured and used by NextAuth.js.
639
635
640
636
#### Example
637
+
641
638
In this example, the custom base path used is `/custom-route`.
The **Azure Active Directory Provider** comes with a set of default options:
28
28
29
-
-[Azure Active Directory Provider options](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/providers/azure-ad-b2c.ts)
29
+
-[Azure Active Directory Provider options](https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/providers/azure-ad-b2c.ts)
30
30
31
31
You can override any of the options to suit your own use case.
0 commit comments