|
32 | 32 | - [Custom routes](#custom-routes) |
33 | 33 | - [Testing helpers](#testing-helpers) |
34 | 34 | - [`generateSessionCookie`](#generatesessioncookie) |
| 35 | +- [Programmatically starting interactive login](#programmatically-starting-interactive-login) |
| 36 | + - [Passing authorization parameters](#passing-authorization-parameters-1) |
| 37 | + - [The `returnTo` parameter](#the-returnto-parameter-1) |
| 38 | + - [Redirecting the user after authentication](#redirecting-the-user-after-authentication-1) |
35 | 39 |
|
36 | 40 | ## Passing authorization parameters |
37 | 41 |
|
@@ -60,14 +64,18 @@ The `returnTo` parameter can be appended to the login to specify where you would |
60 | 64 |
|
61 | 65 | For example: `/auth/login?returnTo=/dashboard` would redirect the user to the `/dashboard` route after they have authenticated. |
62 | 66 |
|
| 67 | +> [!NOTE] |
| 68 | +> The URL specified as `returnTo` parameters must be registered in your client's **Allowed Callback URLs**. |
| 69 | +
|
| 70 | + |
63 | 71 | ### Redirecting the user after logging out |
64 | 72 |
|
65 | 73 | The `returnTo` parameter can be appended to the logout to specify where you would like to redirect the user after they have logged out. |
66 | 74 |
|
67 | 75 | For example: `/auth/login?returnTo=https://example.com/some-page` would redirect the user to the `https://example.com/some-page` URL after they have logged out. |
68 | 76 |
|
69 | 77 | > [!NOTE] |
70 | | -> The URLs specified as `returnTo` parameters must be registered in your client's **Allowed Logout URLs**. |
| 78 | +> The URL specified as `returnTo` parameters must be registered in your client's **Allowed Logout URLs**. |
71 | 79 |
|
72 | 80 | ## Accessing the authenticated user |
73 | 81 |
|
@@ -185,6 +193,15 @@ export async function middleware(request: NextRequest) { |
185 | 193 | > [!IMPORTANT] |
186 | 194 | > The `request` object must be passed as a parameter to the `getSession(request)` method when called from a middleware to ensure that any updates to the session can be read within the same request. |
187 | 195 |
|
| 196 | +## Accessing the idToken |
| 197 | +`idToken` can be accessed from the session in the following way: |
| 198 | + |
| 199 | +```js |
| 200 | +const session = await auth0.getSession(); |
| 201 | +const idToken = session.tokenSet.idToken; |
| 202 | +``` |
| 203 | + |
| 204 | + |
188 | 205 | ## Updating the session |
189 | 206 |
|
190 | 207 | The `updateSession` method could be used to update the session of the currently authenticated user in the App Router, Pages Router, and middleware. If the user does not have a session, an error will be thrown. |
@@ -754,31 +771,66 @@ const sessionCookieValue = await generateSessionCookie( |
754 | 771 | ``` |
755 | 772 |
|
756 | 773 |
|
757 | | -## Programmatic Pushed Authentication Requests (PAR) |
| 774 | +## Programmatically starting interactive login |
758 | 775 |
|
759 | | -The method `startInteractiveLogin` can be called with authorizationParams to initiate an interactive login flow. |
760 | | -The code collects authorization parameters on the server side rather than constructing them directly in the browser. |
| 776 | +Additionally to the ability to initialize the interactive login process by redirecting the user to the built-in `auth/login` endpoint, |
| 777 | +the `startInteractiveLogin` method can also be called programmatically. |
761 | 778 |
|
762 | 779 | ```typescript |
763 | | -// app/api/auth/login/route.ts |
764 | 780 | import { auth0 } from "./lib/auth0"; |
765 | 781 | import { NextRequest } from "next/server"; |
766 | 782 |
|
767 | 783 | export const GET = async (req: NextRequest) => { |
768 | | - // Extract custom parameters from request URL if needed |
769 | | - const searchParams = Object.fromEntries(req.nextUrl.searchParams.entries()); |
| 784 | + return auth0.startInteractiveLogin(); |
| 785 | +}; |
| 786 | +``` |
| 787 | + |
| 788 | +### Passing authorization parameters |
| 789 | + |
| 790 | +There are 2 ways to customize the authorization parameters that will be passed to the `/authorize` endpoint when calling `startInteractiveLogin` programmatically. The first option is through static configuration when instantiating the client, like so: |
| 791 | + |
| 792 | +```ts |
| 793 | +export const auth0 = new Auth0Client({ |
| 794 | + authorizationParameters: { |
| 795 | + scope: "openid profile email", |
| 796 | + audience: "urn:custom:api", |
| 797 | + }, |
| 798 | +}); |
| 799 | +``` |
| 800 | + |
| 801 | +The second option is by configuring `authorizationParams` when calling `startInteractiveLogin`: |
| 802 | + |
| 803 | +```ts |
| 804 | +import { auth0 } from "./lib/auth0"; |
| 805 | +import { NextRequest } from "next/server"; |
770 | 806 |
|
| 807 | +export const GET = async (req: NextRequest) => { |
771 | 808 | // Call startInteractiveLogin with optional parameters |
772 | 809 | return auth0.startInteractiveLogin({ |
773 | | - // a custom returnTo URL can be specified |
774 | | - returnTo: "/dashboard", |
775 | 810 | authorizationParameters: { |
776 | | - prompt: searchParams.prompt, |
777 | | - login_hint: searchParams.login_hint, |
778 | | - // Add any custom auth parameters if required |
779 | | - audience: "custom-audience" |
| 811 | + scope: "openid profile email", |
| 812 | + audience: "urn:custom:api", |
780 | 813 | } |
781 | 814 | }); |
782 | 815 | }; |
| 816 | +``` |
783 | 817 |
|
784 | | -``` |
| 818 | +## The `returnTo` parameter |
| 819 | + |
| 820 | +### Redirecting the user after authentication |
| 821 | + |
| 822 | +When calling `startInteractiveLogin`, the `returnTo` parameter can be configured to specify where you would like to redirect the user to after they have completed their authentication and have returned to your application. |
| 823 | + |
| 824 | +```ts |
| 825 | +import { auth0 } from "./lib/auth0"; |
| 826 | +import { NextRequest } from "next/server"; |
| 827 | + |
| 828 | +export const GET = async (req: NextRequest) => { |
| 829 | + return auth0.startInteractiveLogin({ |
| 830 | + returnTo: '/dashboard', |
| 831 | + }); |
| 832 | +}; |
| 833 | +``` |
| 834 | + |
| 835 | +> [!NOTE] |
| 836 | +> The URLs specified as `returnTo` parameters must be registered in your client's **Allowed Callback URLs**. |
0 commit comments